Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5930    Accepted Submission(s): 4146

Problem Description
As we know, Big Number is always troublesome. But it's really important in our ACM. And today, your task is to write a program to calculate A mod B.

To make the problem easier, I promise that B will be smaller than 100000.

Is it too hard? No, I work it out in 10 minutes, and my program contains less than 25 lines.

 
Input
The input contains several test cases. Each test case consists of two positive integers A and B. The length of A will not exceed 1000, and B will be smaller than 100000. Process to the end of file.
 
Output
For each test case, you have to ouput the result of A mod B.
 
Sample Input
2 3
12 7
152455856554521 3250
 
Sample Output
2
5
1521
 
此题需要用到同余定理:
   常用的同余定理有以下三种:
   (1)((A mod m)*(B mod m))mod m ==(A*B)mod m
   (2)((A mod m)+(B mod m))mod m ==(A+B)mod m
   (3)((A mod m)-(B mod m)+ m)mod m ==(A-B)mod m
    在减法中,由于a mod n 可能小于b mod n,需要在结果上加上n.
   我们先了解下如何运用同余定理求余数:
   对大数求余数:

/*例如10000对m求余:
* 10000%m
* ==(10%m*1000%m)%m
* ==(10%m*(10%m*100%m)%m)%m
* ==(10%m*(10%m*(10%m*10%m)%m)%m)%m
* 用代码表示就是:
* 假设10000是字符串长度是len
*/ /*
* 如123对m求余
* 123%m
* ==((12%m*10%m)%m+3%m)%m
* ==(((10%m+2%m)%m*10%m)%m+3%m)%m
* ==((((1%m*10%m)%m+2%m)%m*10%m)%m+3%m)%m
*/
gets(str);
int ans=0;
for(i=0;i<len;i++)
{
ans=ans*10+str[i];
ans=ans%m;
}

对幂求余数:

/*
* 对幂取模如对37的4次方取模
* (37*37*37*37)%m
* ==(37%m*(37*37*37)%m)%m
* ==(37%m*(37%m*(37*37)%m)%m)%m
* ==(37%m*(37%m*(37%m*37%m)%m)%m)%m
*/
//求n^m%1000
s=n;
for(i=1;i<m;i++)
{
s=s*n;
s=s%1000;
}

详细的模运算请参考:http://blog.csdn.net/chocolate_22/article/details/6458029

此题用到了(1)(2)两个:
#include<stdio.h>
#include<string.h>
#define MAX 1100
int main()
{
int n,m,j,i,s,t;
char p[MAX];
while(scanf("%s",p)!=EOF)
{
scanf("%d",&n);
int l=strlen(p);
s=0;
for(i=0;i<l;i++)
{
s=s*10+p[i]-'0';
s=s%n;
}
printf("%d\n",s);
}
return 0;
}

如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】的更多相关文章

  1. POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15767   A ...

  2. hdu 4704 同余定理+普通快速幂

    此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...

  3. [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   A ...

  4. POJ 1465 Multiple (BFS,同余定理)

    id=1465">http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K T ...

  5. ACM选修HUST1058(市赛题) Lucky Sequence 同余定理

    Description Edward  得到了一个长度为  N  的整数序列,他想找出这里面有多少个“幸运的”连续子序列.一个连续子序列被称为“幸运的”,当且仅当该子序列内的整数之和恰好是  K  的 ...

  6. 2016湖南省赛----A 2016 (同余定理)

    2016湖南省赛----A 2016 (同余定理) Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 ...

  7. HDU-1163Eddy's digital Roots,九余定理的另一种写法!

    下午做了NYOJ-424Eddy's digital Roots后才正式接触了九余定理,不过这题可不是用的九余定理做的.网上的博客千篇一律,所以本篇就不发篇幅过多介绍九余定理了: 但还是要知道什么是九 ...

  8. HDU-2817,同余定理+快速幂取模,水过~

    A sequence of numbers                                                             Time Limit: 2000/1 ...

  9. 十二届 - CSU 1803 :2016(同余定理)

    题目地址:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1803 Knowledge Point: 同余定理:两个整数a.b,若它们除以整数m所 ...

随机推荐

  1. mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句

    http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases;           ...

  2. 4069: [Apio2015]巴厘岛的雕塑

    Description 印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有 N 座雕塑,为方便起见,我们把这些雕塑从 1 到 N 连续地进行标号,其中第 i 座雕塑的年龄 ...

  3. AndroidStudio Gradle版本不匹配问题

    报错信息: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } Error:(1, 1) A problem occurr ...

  4. 关于OJ的输入和输出(转)

    ACM竞赛之输入输出以下内容来源于互联网.在ACM程序设计竞赛中,一道题目的所有测试数据是放在一个文本文件中,选手将一道题目的程序提交给评判系统运行,程序从该文件中读取测试数据,再把运行结果输出到另一 ...

  5. extjs中第一次访问有效,第二次访问出现部分组件无法显示的,动态改变组件的label值的方法,ExtJs中组件最好少使用ID属性(推荐更多使用Name属性)

    在公司做的一个OA项目中,曾经就遇到了这样的一个问题:(我是在jsp中的div中将js render到div中去的)第一次访问此界面的时候,formpanel上的组件能正常显示,不刷新整个页面的前提下 ...

  6. JAVA 时间差距,两个时间相差多少天,时,分,秒

    JAVA 时间差距,两个时间相差多少天,时,分,秒 package io; import java.text.DateFormat; import java.text.ParseException; ...

  7. 使用ImageMagick和Tesseract进行简单数字图像识别

    使用ImageMagick和Tesseract进行简单数字图像识别 由于直接使用 tesseract 进行识别,识别率很低, ImageMagick 安装.配置及使用: 平台:winXP 1. 安装I ...

  8. python is == 的区别

    要点: is 判断是否是同一个对象.是通过id来判断的    == 是通过值来判断的    为了提高内存利用率对一些简单的对象,如一些数值较小的int对象,python采用重用对象内存的方法 例如指向 ...

  9. matlab中 hold on 与hold off的用法

    matlab中 hold on 与hold off的用法 hold on 是当前轴及图形保持而不被刷新,准备接受此后将绘制 hold off 使当前轴及图形不在具备被刷新的性质 hold on 和ho ...

  10. 移除Ubuntu“下载额外数据不成功”的提示通知

    参考自经过几天的摸索,终于得出安装flashplugin-installer的方法 移除"下载额外数据不成功"的恼人提示通知,方法: $cd /usr/share/package- ...