Codevs 1331 西行寺幽幽子(高精度)

1331 西行寺幽幽子
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题目描述 Description
在幻想乡,西行寺幽幽子是以贪吃闻名的亡灵。不过幽幽子可不是只会吃,至少她还管理着亡灵界。话说在幽幽子居住的白玉楼有一颗常年不开花的樱树——西行妖。幽幽子决定去收集人间的春度,聚集起来让西行妖开花。很快,作为幽幽子家园艺师的魂魄妖梦收集到了M个单位的春度。并且在这段时间里,幽幽子计算出要让西行妖开出一朵花需要N个单位的春度。现在幽幽子想要知道,使用所有的春度,能够让西行妖开出多少朵花。
输入描述 Input Description
第1行:一个正整数M
第2行:一个正整数N
N,M的位数不超过L,L的范围在题目后面给出
输出描述 Output Description
第1行:一个整数ans,表示能开出花的朵数
样例输入 Sample Input
73861758
12471
样例输出 Sample Output
5922
数据范围及提示 Data Size & Hint
对于60%的数据:L <= 2,000且ans <= 2,000
对于100%的数据:L <= 20,000且ans <= 2,000,000,000
分类标签 Tags
开放性试题 高精度

/*
这题一看题面.
喜gay烧酒高精除.
其实我们可以二分一个答案做高精度乘法检验.
恩就是这样.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 40001
#define INF 2000000000
#define LL long long
using namespace std;
int a[MAXN],c[MAXN],ans,n[MAXN],m[MAXN],l1,l2;
char tmp[MAXN],tmp1[MAXN];
bool jd(int l)
{
    if(l>n[0]) return false;
    else if(l<n[0]) return true;
    for(int i=n[0];i>=1;i--)
    {
        if(c[i]==n[i]) continue;
        if(c[i]>n[i]) return false;
        if(c[i]<n[i]) return true;    
    }
}
bool check(int x)
{
    int t=0;
    memset(a,0,sizeof(a));
    memset(c,0,sizeof(c));
    while(x){
        a[++a[0]]=x%10;
        x/=10;
    }
    for(int i=1;i<=a[0];i++)
    {
      int x=0;
      for(int j=1;j<=m[0];j++)
      {
          c[i+j-1]+=a[i]*m[j];
        c[i+j]+=c[i+j-1]/10;
        x=c[i+j-1]/10;
        c[i+j-1]%=10;
      }
      c[i+m[0]]=x;
    }
    int l=a[0]+m[0];
    while(!c[l]&&l>=1) l--;
    if(jd(l)) return true;
    else return false;
}
void erfen(LL l,LL r)
{
    int mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(check(mid)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    printf("%d",ans);
}
void slove()
{
    for(int i=1;i<=l1;i++)
      n[i]=tmp[l1-i+1]-48;
    for(int i=1;i<=l2;i++)
      m[i]=tmp1[l2-i+1]-48;
    n[0]=l1,m[0]=l2;
}
int main()
{
    scanf("%s",tmp+1);l1=strlen(tmp+1);
    scanf("%s",tmp1+1);l2=strlen(tmp1+1);
    slove();
    erfen(1,INF);
    return 0;
}
原文地址:https://www.cnblogs.com/nancheng58/p/10068214.html