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. AppStore 审核中文版 --- 程序员必看

    App Store审核指南中文版(2014.10.11更新) 2014-11-03 程序猿 苹果在9月3日对App Store审核指南进行了重大更新,新添加了扩展.HealthKit.HomeKit以 ...

  2. Servlet的一些API使用介绍

    final String rootPath = getServletConfig().getServletContext().getRealPath("/");  获取项目运行的根 ...

  3. python 重载 __hash__ __eq__

    __author__ = 'root' from urlparse import urlparse class host_news(): def __init__(self, id, url): se ...

  4. duilib入门简明教程 -- VS环境配置(2) Alberl

      既然是入门教程,那当然得基础点,因为搜索duilib相关资料时,发现有些小伙伴到处都是编译错误,以及路径配置错误等等,还有人不知道SVN,然后一个个文件手动下载的.     其实吧,duilib的 ...

  5. 任正非:华为三十年大限快到了 想不死就得新生(建立战略预备队)cool

    华为心声社区官方微信今日发布了任正非8月15日在华为公司内部做的关于战略预备队建设汇报的讲话.讲话内容中提到,华为公司需要组织.结构.人才等所有一切都变化,通过变化使新的东西成长起来.   任正非表示 ...

  6. 没做过编译器就是被人欺——从一道变态的i++题猜编译器的行为(表达式从左往右扫描,同一变量相互影响)

    首先不要被人蒙了,如果是这样,根本编译不过: int i=1; int b=i+++++i; printf("%d %d\n", b ,i); Mingw报错:error: lva ...

  7. 【Xamarin开发 Android 系列 7】 Android 结构基础(下)

    原文:[Xamarin开发 Android 系列 7] Android 结构基础(下) *******前期我们不打算进行太深入的东西,省的吓跑刚进门的,感觉门槛高,so,我们一开始就是跑马灯一样,向前 ...

  8. 文件已经加入.gitignore但是vs并没有显示文件处于ignore状态

    在VS2015的项目文件中看到某些文件的状态比较特殊, 前面被标记了红色的标志, 如下图. 本来以为这是通过VS修改文件属性做到的, 但是光标移到文件上发现显示的是Ignore, 才知道是被git所忽 ...

  9. android Button隐藏

    两种方式: xml方式 和 java代码方式: 可见(visible) XML文件:android:visibility="visible" Java代码:view.setVisi ...

  10. 织梦内容管理系统(DedeCms) 小说模块insert注入漏洞

    漏洞版本: Dedecms 漏洞描述: DedeCms是免费的PHP网站内容管理系统. 织梦内容管理系统(DedeCms) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户 ...