B-Casting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 449    Accepted Submission(s): 223

Problem Description
Casting around for problems leads us to combine modular arithmetic with different integer bases, particularly the problem of computing values modulo b-1, where b is the base in which the value is represented. For example,

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.

 
Input
The first line of input contains a single integer P, (1 <= P <= 1000) , which is the number o data sets that follow. Each data set should be processed identically and independently.

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.

 
Output
For each data set there is a single line of output. It contains the data set number followed by a single space which is then followed by the remainder resulting from dividing D by (B-1).

 
Sample Input
4
1 10 7829
2 7 123456
3 6 432504023545112
4 8 37777777777777773
 
Sample Output
1 8
2 3
3 1
4 6
 
这题没啥好说的,水题,用字符串保存那个数,然后把它转化成十进制再mod给的那个值,由于这个数太大所以每计算一步就要mod一下,这样就没问题了
#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;
}

最新文章

  1. [转]Ext ComboBox 默认选中某一项
  2. [Machine Learning] 机器学习常见算法分类汇总
  3. Could not load file using Ranorex runtime : General Questions
  4. 转数据库Sharding的基本思想和切分策略
  5. 第三百五十五天 how can I 坚持
  6. Codeforces Gym 100015C City Driving 离线LCA
  7. YII中面包屑制作(当前位置:网站首页 &gt;&gt; 会员登陆)
  8. java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
  9. 关于ognl.OgnlException: target is null for setProperty(null的解决方案
  10. ps快速删除圆角图片旁白的白色区域方法
  11. JAVA用JNI方法调用C代码实现HelloWorld
  12. linux下面安装配置mongoDB
  13. jquery模板下载网站
  14. iPhone X 网页导航概念
  15. 关于AI
  16. PAT甲级 1001 A+B Format
  17. 关于正餐智能POS6.0.1.1改版后,订单模块无法进行部分退款的FAQ
  18. 如何确定系统上的CPU插槽数量
  19. UI5-学习篇-17-云端WEB IDE开发
  20. KVM虚拟化图

热门文章

  1. We7在政府门户中的应用
  2. .NET笔试题(关于迭代的:遍历XML中的FileName)
  3. webservice 的权限验证
  4. 实现一个JavaScript模块化加载器
  5. 【POJ3169 】Layout (认真的做差分约束)
  6. selectpicker下拉多选框ajax异步或者提前赋值=》默认值
  7. 子查询解嵌套in改写为exists
  8. Android 性能优化 四 布局优化merge标签的使用
  9. BZOJ2296: 【POJ Challenge】随机种子
  10. [C# 网络编程系列]专题六:UDP编程