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. 向OC类中添加默认的协议实现(ProtocolKit)

    以forkingdog的PorotocolKit举例 举例 ProtocolKit Protocol extension for Objective-C Usage Your protocol: @p ...

  2. iOS开发之自定义控制器切换

    iOS8以后, 苹果公司推出了UIPresentationController, 通过其(presentedController 和 presentingController)来控制modal控制器操 ...

  3. MySQL简单使用

    1.启动MySQL服务器实际上上篇已讲到如何启动MySQL.两种方法:一是用winmysqladmin,如果机器启动时已自动运行,则可直接进入下一步操作.二是在DOS方式下运行 d:/mysql/bi ...

  4. 15 个响应式的 jQuery 图像滑块插件

    设计师和开发人员总是试图使用新技术让网站更智能,而我们发现在许多网站上 jQuery 的图像滑块插件是非常受欢迎的.本文继续介绍 15 个 jQuery 图像滑块插件以供您选择. ELASTISLID ...

  5. C#函数的方法定义和方法调用小议

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  6. QLGame 2d Engine Android端环境搭建(通过jni读取assets目录的文件)

    QLGame 2d Engine win端已经实现了一个动物的动画了,初步的东西已经完成,考虑在Android端也实现这些基本的东西,这样跨平台的引擎也就实现了! 要在Android下编程,首先要实现 ...

  7. 李洪强漫谈iOS开发[C语言-026]-符合赋值表达式

  8. [LeetCode] Burst Balloons (Medium)

    Burst Balloons (Medium) 这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高: 初始化maxCount = 0. 对于当前长度为k的数组nums, 从0到k - 1逐 ...

  9. centos挂载移动硬盘

    yum install ntfs-3g sudo mount -t ntfs-3g /dev/sdc1 /mnt/mobiledisk https://tuxera.com/opensource/nt ...

  10. bzoj1799

    这是一道比较难的数位dp 因为逐位统计好像无法处理数位和整除原数的 但是有了刚才的bzoj1072的经验,我们能做的是逐位处理被一个数d整除的方案 不难想到先穷举数位和now,now最大也就162,可 ...