zhx's submissions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2304 Accepted Submission(s): 645
Problem Description
As one of the most powerful brushes, zhx submits a lot of code on many oj and most of them got AC.
One day, zhx wants to count how many submissions he made on n ojs. He knows that on the ith oj, he made ai submissions. And what you should do is to add them up.
To make the problem more complex, zhx gives you n B−base numbers and you should also return a B−base number to him.
What's more, zhx is so naive that he doesn't carry a number while adding. That means, his answer to 5+6 in 10−base is 1 . And he also asked you to calculate in his way.
One day, zhx wants to count how many submissions he made on n ojs. He knows that on the ith oj, he made ai submissions. And what you should do is to add them up.
To make the problem more complex, zhx gives you n B−base numbers and you should also return a B−base number to him.
What's more, zhx is so naive that he doesn't carry a number while adding. That means, his answer to 5+6 in 10−base is 1 . And he also asked you to calculate in his way.
Input
Multiply test cases(less than
1000
). Seek
EOF
as the end of the file.
For each test, there are two integers n and B separated by a space. ( 1≤n≤100 , 2≤B≤36 )
Then come n lines. In each line there is a B−base number(may contain leading zeros). The digits are from 0 to 9 then from a to z (lowercase). The length of a number will not execeed 200.
For each test, there are two integers n and B separated by a space. ( 1≤n≤100 , 2≤B≤36 )
Then come n lines. In each line there is a B−base number(may contain leading zeros). The digits are from 0 to 9 then from a to z (lowercase). The length of a number will not execeed 200.
Output
For each test case, output a single line indicating the answer in
B−base
(no leading zero).
Sample Input
2 3 2 2 1 4 233 3 16 ab bc cd
Sample Output
1 233 14
思路:直接模拟
注意一个数的情况
输出处理前导0
数组的初值
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1100
int ans[N];
int f[N][N],cou[N];
int main()
{
int n,b;
char op[N];
while(~scanf("%d %d",&n,&b))
{
int maxn=-1;
memset(cou,0,sizeof(cou));
for(int i=1; i<=n; i++)
{
scanf("%s",op);
int len=strlen(op);
int k=0;
while(k<len&&op[k]=='0') k++;
if(k==len) continue;
cou[i]=len-k;
maxn=max(maxn,len-k);
for(int j=k; j<len; j++)
{
if(op[j]>='0'&&op[j]<='9')
f[i][j-k]=op[j]-'0';
else f[i][j-k]=op[j]-'a'+10;
}
}
if(maxn==-1) printf("0\n");
else
{
memset(ans,0,sizeof(ans));
for(int i=1; i<=n; i++)
{
int q=maxn-cou[i];
for(int j=cou[i]-1; j>=0; j--)
f[i][j+q]=f[i][j];
for(int j=0; j<q; j++)
f[i][j]=0;
}
for(int i=1; i<=n; i++)
for(int j=0; j<maxn; j++)
ans[j]=(ans[j]+f[i][j])%b;
int num=-1;
for(int i=0; i<maxn; i++)
if(ans[i]!=0)
{
num=i;
break;
}
if(num==-1)
{
printf("0\n");
continue;
}
for(int i=num; i<maxn; i++)
{
if(ans[i]<10) printf("%d",ans[i]);
else printf("%c",ans[i]-10+'a');
}
printf("\n");
}
}
return 0;
}