Calculate the Sum
Time Limit: 1000MS Memory Limit: 10000KB Submissions: 72 Accepted: 17
Description
As you all know, MOD is a mathematical operatio. Giving you two numbers n,m(0 < m,n <= 10^10001),Your task is to calculate the sum of every digit of m MOD every digit of n. We can guarantee that there is no zero in digits of n.
Input
The first line, a integer T, representing T test cases blew.(T<=10).
In each case, the first line is the integer m, the second line is the integer n.
Output
Print the sum of every digit of m MOD every digit of n.
Sample Input
Sample Output
Hint
Source
SWUST 7th ACM/ICPC Programming Contest - 2011
数据很大,10^10001次方,如果直接2重循环去遍历,肯定超过1ms,所以必须巧妙处理,考虑到每位的数字是0到9之间的,可以开一个数组,记录一下前一个数据中0到9这些数字分别有几个,这样就可以降低复杂度了。
#include<stdio.h> #include<string.h> char a[10001],b[10001]; int main() { int t,i,j,sum,lena,lenb,c[10]; scanf("%d",&t); while(t--) { sum=0; memset(c,0,sizeof(c)); scanf("%s%s",a,b); lena=strlen(a); lenb=strlen(b); for(i=0;i<lena;i++) ++c[a[i]-'0']; for(i=0;i<lenb;i++) for(j=0;j<10;j++) sum+=j%(b[i]-'0')*c[j]; printf("%d\n",sum); } }