(大数取模)Big Number hdu1212
Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9006 Accepted Submission(s): 6100
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
(ABC)%n=(A*100%n+B*10%n+C%n)%n
(A*B)%n=(A%n*B%n)%n
于是,可以利用循环,对于挺大的数,利用字符串来表示这个数。
比如,令ABC为字符串,则有
1)sum=A%n;
2)A%n*10+B%n=sum+B%n
于是sum=(sum+B%n)%n
3)经过循环,会有:
sum=(sum+C%n)%n=((sum+B%n)%n*10+C%n)%n=((A%n*10+B%n)%n*10+C%n)%n=((A*100%n+B*10%n)%n+C%n)%n=(A*100%n+B*10%n+C%n)%n
故而可以用循环求解。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
int b,len,sum;
while(cin>>a>>b)
{
len=a.length();
sum=;
for(int i=;i<len;i++)
sum=(sum*+(a[i]-'')%b)%b;
cout<<sum<<endl;
}
return ;
}
用JAVA更简单
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int n;
BigInteger a,b;
while(in.hasNextBigInteger()) {
a=in.nextBigInteger();
b=in.nextBigInteger();
a=a.remainder(b);
System.out.println(a);
}
}
}
(大数取模)Big Number hdu1212的更多相关文章
- HDU--1212大数取模
大数取模问题.题目传送门:HDU1212 #include <iostream> using namespace std; char a[1010]; int main() { int b ...
- 题解报告:hdu 1212 Big Number(大数取模+同余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 Problem Description As we know, Big Number is al ...
- (POJ2635)The Embarrassed Cryptographer(大数取模)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...
- 【大数取模】HDOJ-1134、CODEUP-1086
1086: 大数取模 题目描述 现给你两个正整数A和B,请你计算A mod B.为了使问题简单,保证B小于100000. 输入 输入包含多组测试数据.每行输入包含两个正整数A和B.A的长度不超过1 ...
- hdu2302(枚举,大数取模)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1 ...
- HDU4704Sum 费马小定理+大数取模
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4704 题目大意: 看似复杂,其实就是求整数n的划分数,4=1+1+2和4=1+2+1是不同的.因而可 ...
- ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(隔板定理 + 小费马定理 + 大数取模,组合数求和)题解
题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 :所以3共有4种),n最多有1e5位,答案取模p = 1e9+7 思路:就是往n个东西中间插任意个板子 ...
- HPU 1471:又是斐波那契数列??(大数取模)
1471: 又是斐波那契数列?? 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 27 统计 题目描述 大家都知道斐波那契数列吧?斐波那契数列的定义是这样的: f0 = 0; ...
- HDU-2303 The Embarrassed Cryptographer 高精度算法(大数取模)
题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取 ...
- HDU-1226-超级密码-队列+广搜+大数取模
Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进制的数,并且只能由给定的M个数字构成,同 ...
随机推荐
- 《Linux内核分析》实践2
<Linux及安全>实践2 一.Linux基本内核模块 1.1什么是内核模块 linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一 ...
- Oracle系列(一): Oracle数据恢复
Oracle数据恢复 在使用Oracle的时候,突然一部小心update或者delete全部数据后怎么办? select * from table as of timestamp to_timest ...
- 实战框架ABP
abp及实战框架概述 接触abp也快一年了,有过大半年的abp项目开发经验,目前项目中所用的abp框架版本为0.10.3,最新的abp框架已经到了1.4,并且支持了asp.net core.关于abp ...
- PAT 1039 到底买不买
https://pintia.cn/problem-sets/994805260223102976/problems/994805283241443328 小红想买些珠子做一串自己喜欢的珠串.卖珠子的 ...
- Oracle12c Clone PDB 的方法
1. 创建PDB的存放路径,举例: 2. 设置 数据库创建数据文件的目录 alter system set db_Create_file_dest='C:\app\Administrator\orad ...
- React 多组件传值props和this
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Python 2和Python 3的编码问题
在Python2中,字符串无法完全地支持国际字符集和Unicode编码.为了解决这种限制,Python2对Unicode数据使用了单独的字符串类型.要输入Unicode字符串字面量,要在第一个引号前加 ...
- awk、sed、grep三大shell文本处理工具之awk的应用
awk 1.是什么 是一个编程语言.支持变量.数组.函数.流程控制(if...else/for/while) 单行程序语言. 2.工作流程 读取file.标准输入.管道给的数据,从第一行开始读取,逐行 ...
- MT【41】利用不等式妙消参数
已知$\theta\in[0,2\pi]$对任意$x\in[0,1],2x^2sin\theta-4x(1-x)cos\theta+3(1-x)^2>0$恒成立.求$\theta$的范围. 解答 ...
- Real mode & Protected mode
[转] https://objectkuan.gitbooks.io/ucore-docs/content/lab1/lab1_3_2_1_protection_mode.html 为何要了解Int ...