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. Xcode 证书生成、设置、应用

    Xcode 证书生成.设置.应用,与大家分享.如果按下面步骤还不能编译成功,我手把手教你. 开发环境: Mac OS lion 10.7.4 XCode 4.3.3 1         点击钥匙图标 ...

  2. jQuery预加载插件

      插件描述:jQuery Fadeloader的插件可以让你轻松实现预加载到您的网站或部分使用级联渐显效果来显示特定的内容块(例如,头> MENU>内容>页脚) jQuery Fa ...

  3. 告别无止境的增删改查--Java代码生成器

    转自:http://www.cnblogs.com/zhuYears/archive/2012/02/29/2373491.html 告别无止境的增删改查--Java代码生成器 有感于马上要做个比较大 ...

  4. 单位有b\B\K\M\G的相互转换

    计算机存储计量单位 1. 计算机最小存储计量单位是:BIT(位) 2. 计算机最基本存储计量单位是:Bytes(字节) 3. Bit和Bytes的关系:8Bit=1Bytes 4. 其他常用单位:1K ...

  5. [转贴]实践:C++平台迁移以及如何用C#做C++包装层

    终于有个C++ 如何调用C#类库的文章,收藏之 在前面,我们看过OpenTK与MOgre,这二个项目都是C#项目,但是他的实现都是C++.他们简单来说就是一个包装层.常见的包装方式有二种,一 种就是我 ...

  6. 水平/竖直居中在旧版Safari上的bug

    今天调了两个出现在旧版Safari上的layout bug. 它们最初是在同事的iPad上被发现的, 我在自己桌面上安装的Safari 5.1.7上也能够复现. Bug1: .vertical-cen ...

  7. 火车车次查询-余票查询--Api接口

    1.来自12306的火车车次数据 使用12306网站的接口,查询余票.此接口采集自 这里. 全国火车站代号字典,下载 . 火车票余票查询 http://dynamic.12306.cn/otsquer ...

  8. c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制

    using mshtml;using System;using System.Collections.Generic;using System.Linq;using System.Security.P ...

  9. 【转】三十三、Android给ListView设置分割线Divider样式

    原文网址:http://www.cnblogs.com/linjiqin/archive/2011/11/12/2246349.html 给ListView设置分割线,只需设置如下两个属性: andr ...

  10. Android-获取外置SDcard路径

    Android手机支持SDcard.目前很多手机厂商把SDcard集成到手机中,当然有的手机同时也支持可插拔的SDcard.这就有了内置SDcard和位置SDcard之分.当手机同时支持内置和外置SD ...