B-Casting
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 449 Accepted Submission(s): 223
7829
10 mod 9 = 8,
37777777777777773
8 mod 7 = 6
123456
7 mod 6 = 3
(Note that 37777777777777773
8 = 1125899906842619
10 and 123456
7 = 22875
10.)
Your job is to write a program that reads integer values in various bases and computes the remainder after dividing these values by one less than the input base.
Each data set consists of a single line of input containing three space-separated values. The first is an integer which is the data set number. The second is an integer which is the number, B (2 <= B <= 10), denoting a numeric base. The third is an unsigned number, D, in base B representation. For this problem, the number of numeric characters in D will be limited to 10,000,000.
1 10 7829
2 7 123456
3 6 432504023545112
4 8 37777777777777773
2 3
3 1
4 6
#include<stdio.h>
#include<string.h>
char s[10000005]; int main()
{
int i,j,n,x,t;
__int64 sum;
scanf("%d",&n);
while(n--)
{
scanf("%d%d%s",&t,&x,s);
j=strlen(s);
for(i=0,sum=0;i<j;i++)
{
sum=(sum*x+s[i]-'0')%(x-1);//每次计算都mod(x-1)
}
printf("%d %I64d\n",t,sum);
}
return 0;
}
上面那个好理解,但是跑了1000多ms,内存7000多k,再贴一个跑到前几名的代码,234ms,220k
#include<stdio.h>
#include<string.h>
int main()
{
int n,x,t;
int sum;
char c;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&t,&x);
getchar();
sum=0;
while((c=getchar())!='\n')//这里没存那个数
{
sum=(sum*x+c-'0');
if(sum>1000000)
sum%=(x-1);
}
printf("%d %d\n",t,sum%(x-1));
}
return 0;
}