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

hdu4485 B-Casting(mod运算)的更多相关文章

  1. [HASH]MOD运算用户哈希函数

    一.概述 MOD(取模)运算配合质数的特性,可以实现一种简单的哈希算法. 二.基于的定理 在理解如何实现mod哈希前应当了解一些数学的定理: 1.x mod y = z ,实际上是x除以y的余数y的意 ...

  2. JS中的MOD运算

    最近研究汉诺塔非递归的时候,看到书上写了个MOD,久违啊,感觉好久没看到过了,都忘了怎么用了. 某人:我知道,这不就是取余嘛,直接%就行了. 嗯......,如果是python语言,你说的很对,但是我 ...

  3. mod 运算与乘法逆元

    mod 运算与乘法逆元 %运算 边乘边mod 乘法 除法 mod 希望计算5/2%7=6 乘法 除法 mod 希望计算5/2%7=6 两边同时/x 在取mod(p)运算下,a/b=a*bp-2 bp- ...

  4. Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task 数学 mod运算的性质

    C. Ehab and a 2-operation task 数学 mod运算的性质 题意: 有两种对前缀的运算 1.对前缀每一个\(a +x\) 2.对前缀每一个\(a\mod(x)\) 其中x任选 ...

  5. hdu1576 mod 运算的逆元

    Problem Description 要求(A/B)%9973,但因为A非常大,我们仅仅给出n(n=A%9973)(我们给定的A必能被B整除.且gcd(B,9973) = 1).   Input 数 ...

  6. a ^ b mod c 取模运算优化反思(老物)

    这是一篇嘲讽我之前的自己采用笨重愚蠢思想去解决问题的日志. RSA 加密与解密涉及到 a ^ b mod c 的问题,如何计算这个值呢? 我会选择 pow(a, b) % c, 事实上在写RSA的时候 ...

  7. C入门---位运算

    程序中的所有数在计算机内存中都是以二进制的形式储存的.位运算直接对整数在内存中的二进制位进行操作.由于位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快. (1),与(&)运算 ...

  8. Java千百问_03基本的语法(005)_二进制是如何做位运算的

    点击进入_很多其它_Java千百问 二进制是如何做位运算的 程序中的全部数在计算机内存中都是以二进制的形式储存的.位运算说白了,就是直接对整数在内存中的二进制位进行操作. 其它运算符看这里:java种 ...

  9. BZOJ 1876: [SDOI2009]SuperGCD

    1876: [SDOI2009]SuperGCD Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 3060  Solved: 1036[Submit][St ...

随机推荐

  1. Web负载均衡的几种方式

    Web负载均衡的几种实现方式 摘要:负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用 ...

  2. sql server2005主从数据库同步配置

    网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库.主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据的 ...

  3. UIVIewController自定义切换效果-b

      之前介绍动画时提过UIView的转场动画,但是开发中我们碰到更多的viewController的切换,ios中常见的viewcontroller切换有四种:模态视图,导航栏控制器,UITabBar ...

  4. BZOJ 1828: [Usaco2010 Mar]balloc 农场分配

    Description Input 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i ...

  5. SQL Server 2005 版本的操作系统兼容性详细列表

    操作系统要求(32 位) 此表显示对于每种 32 位版本的 SQL Server 2005,操作系统是否可以运行其服务器软件. 有关如何在 Windows Server 2008 上安装 SQL Se ...

  6. USACO3.34Home on the Range(DP)

    之前做过一道类似的 国际象棋盘神马的.. 统计出以每个1作为右下角的最大正方形 那么以大于二到这个最大值之间为边的正方形都可以以这个为右下角 累加就可以了 dp[i][j] = min(dp[i-1] ...

  7. ImageSwitcher 右向左滑动的实现方式

    ImageSwitcher is;...is.setInAnimation(this, android.R.anim.slide_in_left);is.setOutAnimation(this, a ...

  8. I Hate It HDOJ---1754

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. [4X]荣耀畅玩4X开箱实录

    http://www.jianshu.com/p/8d171c389ee8 文字都在简书里面啦~~

  10. python进度1

    Python 错误和异常 异常参数: 3.4与2.7有些不同 3.4中 try: x except NameError as e: print(type(e)) print(e) 运行结果: < ...