Calculate the Sum

Calculate the Sum


                            Time Limit: 1000MS Memory Limit: 10000KB
Submissions: 72 Accepted: 17

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.

Sample Input

1
13
21

Sample Output

2

 

數據很大,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); 
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章