Calculate the Sum

Calculate the Sum


                            Time Limit: 1000MSMemory Limit: 10000KB
Submissions: 72Accepted: 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

1
13
21

Sample Output

2
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); 
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值