hdu5832 A water problem (大数求余)

    xiaoxiao2025-07-20  9

    hdu5832 A water problem (大数求余):http://acm.hdu.edu.cn/showproblem.php?pid=5832

    题目描述:

    合训练的同学们~ 

    A water problem

    Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 365    Accepted Submission(s): 201 Problem Description Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is  73  days in Xixi a year and  137  days in Haha a year.  Now you know the days  N  after Big Bang, you need to answer whether it is the first day in a year about the two planets.   Input There are several test cases(about  5  huge test cases). For each test, we have a line with an only integer  N(0N) , the length of  N  is up to  10000000 .   Output For the i-th test case, output Case #i: , then output "YES" or "NO" for the answer.   Sample Input 10001 0 333   Sample Output Case #1: YES Case #2: YES Case #3: NO

    题目大意:

    输入一个数n,n为一个大数,若n既是73的倍数又是137的倍数,则输出YES,否则输出NO。

    题目分析:

    由于73和137都是质数,所以,若n为73*137的倍数,则输出YES,否则输出NO。但是由于n为大数,10的7次方以内的数,则直接开10的7次方的char数组进行输入,还要把char数组中的内容对应到int数组中,会内存超限,同样用java语言交的话,也会内存超限,为了不超限,有两种处理的方法。

    代码实现一:

    #include <cstdio> #include <cstring> const int N=1000010; typedef long long LL ; LL a[N]={0}; char a1[N*10]; int main() { int i,j,k; LL d,b; int T=0; while(scanf("%s",a1)!=-1) { T++; //if (strcmp(a1,"0")==0) {printf("Case #%d: YES\n",T);continue;} d=0; k=strlen(a1); //printf("kkk%d\n",k); for(i=0,j=0; i<k; j++) { if (i+10<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+((LL)a1[k-i-3]-'0')*100+(LL)(a1[k-i-4]-'0')*1000+(LL)(a1[k-i-5]-'0')*10000+(LL)(a1[k-i-6]-'0')*100000+(LL)(a1[k-i-7]-'0')*1000000+(LL)(a1[k-i-8]-'0')*10000000+(LL)(a1[k-i-9]-'0')*100000000+(LL)(a1[k-i-10]-'0')*1000000000; i+=10; } else if (i+10>k && i+9<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+(LL)(a1[k-i-3]-'0')*100+(LL)(a1[k-i-4]-'0')*1000+(LL)(a1[k-i-5]-'0')*10000+(LL)(a1[k-i-6]-'0')*100000+(LL)(a1[k-i-7]-'0')*1000000+(LL)(a1[k-i-8]-'0')*10000000+(LL)(a1[k-i-9]-'0')*100000000; i+=9; } else if (i+9>k && i+8<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+(LL)(a1[k-i-3]-'0')*100+(LL)(a1[k-i-4]-'0')*1000+(LL)(a1[k-i-5]-'0')*10000+(LL)(a1[k-i-6]-'0')*100000+(LL)(a1[k-i-7]-'0')*1000000+(LL)(a1[k-i-8]-'0')*10000000; i+=8; } else if (i+8>k && i+7<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+(LL)(a1[k-i-3]-'0')*100+(LL)(a1[k-i-4]-'0')*1000+(LL)(a1[k-i-5]-'0')*10000+(LL)(a1[k-i-6]-'0')*100000+(LL)(a1[k-i-7]-'0')*1000000; i+=7; } else if (i+7>k && i+6<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+(LL)(a1[k-i-3]-'0')*100+(LL)(a1[k-i-4]-'0')*1000+(LL)(a1[k-i-5]-'0')*10000+(LL)(a1[k-i-6]-'0')*100000; i+=6; } else if (i+6>k && i+5<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+(LL)(a1[k-i-3]-'0')*100+(LL)(a1[k-i-4]-'0')*1000+(LL)(a1[k-i-5]-'0')*10000; i+=5; } else if (i+5>k && i+4<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+(LL)(a1[k-i-3]-'0')*100+(LL)(a1[k-i-4]-'0')*1000; i+=4; } else if (i+4>k && i+3<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10+(LL)(a1[k-i-3]-'0')*100; i+=3; } else if (i+3>k && i+2<=k) { a[j]=(LL)(a1[k-i-1]-'0')+(LL)(a1[k-i-2]-'0')*10; i+=2; } else if (i+2>k && i+1<=k) { a[j]=(LL)(a1[k-i-1]-'0'); i++; } } // for(int i=j-1; i>=0; i--) // { // printf("%I64d ",a[i]); // } // printf("\n"); for(i=j-1; i>=0; i--) { d=d*10000000000+a[i]; d=d%10001; } if (d==0) printf("Case #%d: YES\n",T); else printf("Case #%d: NO\n",T); } return 0; } 代码实现二:

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int mod=10001; char str[10000100]; int main() { long long sum; int casenum=1; while(scanf("%s",str)!=EOF) { sum=0; int len=strlen(str); for(int i=0;i<len;i++) { sum=sum*10+(str[i]-'0'); sum=sum%mod; } if(sum==0) { printf("Case #%d: YES\n",casenum++); } else { printf("Case #%d: NO\n",casenum++); } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1300868.html
    最新回复(0)