【大数取模】HDOJ-1134、CODEUP-1086】的更多相关文章

1086: 大数取模   题目描述 现给你两个正整数A和B,请你计算A mod B.为了使问题简单,保证B小于100000. 输入 输入包含多组测试数据.每行输入包含两个正整数A和B.A的长度不超过1000,并且0<B<100000. 输出 对于每一个测试样例,输出A mod B. 样例输入 2 3 12 7 152455856554521 3250 样例输出 2 5 1521 [概念] (a+b)%n =(a%n+b%n)%n (a-b)%n = (a%n-b%n)%n 实话说刚开始我没看懂…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1e6):其中k是两个素数的乘积,问k是否存在严格小于l的因子,若有,输出 BAD 该因子,反之输出GOOD: 思路: 先1e6内素数打表,再枚举一个因子,判断因子用大数取模: 代码: #include <iostream> #include <stdio.h> #include <…
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accepted: 3516 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of user…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4704 题目大意: 看似复杂,其实就是求整数n的划分数,4=1+1+2和4=1+2+1是不同的.因而可知答案是2n-1. 题目分析: 因为n实在是太大太大了,这可咋办啊?!n<10100000. 做这场的时候没有注意到,也是当时没有看过什么是费马小定理,居然跟模值有关系!mod=1000000007.这个mod有什么特点呢?它是个质数. 费马小定理揭示了:当p是一个素数并且a和p互质时,ap-1 %…
大数取模问题.题目传送门:HDU1212 #include <iostream> using namespace std; char a[1010]; int main() { int b; while (cin >> a>> b) { int flag = 0; for (int i = 0; a[i] != '\0'; i++) flag = ((flag * 10) % b+ (a[i] - '0') %b) % b; cout << flag <…
题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 :所以3共有4种),n最多有1e5位,答案取模p = 1e9+7 思路:就是往n个东西中间插任意个板子,所以最多能插n - 1个,所以答案为2^(n - 1) % p.但是n最大有1e5位数,所以要用小费马定理化简. 小费马定理:假如p是质数,且gcd(a,p)=1,那么a (p-1)≡1(mod p) 所以我们只要把n - 1分解为n - 1 = k(p - 1) + m,而2^ k(p - 1)…
1471: 又是斐波那契数列?? 时间限制: 1 Sec 内存限制: 128 MB 提交: 278 解决: 27 统计 题目描述 大家都知道斐波那契数列吧?斐波那契数列的定义是这样的: f0 = 0; f1 = 1; fi = fi-1 + fi-2 现在给你一个数x,聪明的你一定知道这是斐波那契数列中的第几项. (数据保证x一定有对应的项y,且 0 <= y < 1e4) 输入 第一行一个整数T,表示测试组数. 之后的T行,每行一个数x 输出 对于每个测试数据,输出一行表示数x是第几项 样例…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 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…
题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取模即可 注意大数取模代码,一开始没想到,看了别人的代码才感觉厉害 代码 #include <cstring> #include <cstdio> const int MAX=int(1e6); char a[100+5]; int b, psize=0, primes[MAX+5];…
Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进制的数,并且只能由给定的M个数字构成,同时密码是一个给定十进制整数N(0<=N<=5000)的正整数倍(如果存在多个满足条件的数,那么最小的那个就是密码),如果这样的密码存在,那么当你输入它以后门将打开,如果不存在这样的密码......那就把门炸了吧. 注意:由于宝藏的历史久远,当时的系统最多只能保存500位密码.因此如果得到的密码长度…
1千万长度的数对73和137取模.(两个数有点像,不要写错了) 效率要高的话,每15位取一次模,因为取模后可能有3位,因此用ll就最多15位取一次. 一位一位取模也可以,但是比较慢,取模运算是个耗时的运算. #include <cstdio> #define ll long long ll n,m; int p,cas; char s[10000005]; int main() { while(gets(s)){ n=m=p=0; while(s[p]){ for(int i=0;i<1…
Big Number 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 ——每天在线,欢迎留言谈论. 题目大意: 给你两个数 n1,n2.其中n1 很大很大,n1%n2的值. 知识点: ①秦九韶公式:例:1314= ((1*10+3)*10+1)*10+4 ②(a*b)%c == (a%c)*(b%c) .(a+b)%c == (a%c)+(b%c) . 思路: 每步取模即可. C++ AC代码: #include <iostream> #…
题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N-1}\),第一朵可以在k个颜色中任意选择,第二朵可以有k-1个选择,第三朵也有k-1.... 但是f(N,k)种方案中包含了使用了少于k个颜色的方案数,要减去这些方案数.设没有使用的颜色数为i,当i=1时,减去只使用1种颜色的方案数\(C(k,1)*f(N,k-1)\);当i=2时,方案数已经被i=…
首先,我们珂以抽象出S函数的模型:把n拆成k个正整数,有多少种方案? 答案是C(n-1,k-1). 然后发现我们要求的是一段连续的函数值,仔细思考,并根据组合数的性质,我们珂以发现实际上答案就是在让求2^(n-1). 然鹅我们并不能高兴地过早.因为n的数量级竟然到了丧心病狂的1e100000.连高精度都救不了它. 费马小定理 费马小定理有两种形式:  $a^{p-1}$≡1($mod$ $p$)   与 $a^p$≡$a$($mod$ $p$). 第二种形式更为通用,是因为第一种形式不能涵盖“$…
Big Number 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 10000…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 题意:给出两个数a, b,求a%b: 思路:(c+d)%e=c%e+d%e,(c*d)%e=(c%e*d%e)%e: 代码: #include <iostream> #include <stdio.h> #include <string.h> #define ll long long #define MAXN 1000+10 using namespace std;…
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 用java写大数果然是方便多了! import java.math.BigInteger; import java.util.*; import java.io.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); int n = c…
题意:输入一个进制b,在输入两个基于b进制的大整数 x,y ,求x%y的b进制结果. http://162.105.81.212/JudgeOnline/problem?id=2305 函数: String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35). int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt…
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 t…
链接:https://www.nowcoder.com/acm/contest/93/K来源:牛客网 题目描述 wyh学长特别喜欢斐波那契数列,F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n>=2) 一天他突发奇想,想求F(a^b)%c 输入描述: 输入第一行一个整数T(1<=T<=100),代表测试组数接下来T行,每行三个数 a,b,c (a,b<=2^64) (1<c<1000) 输出描述: 输出第a^b项斐波那契数对c取余的结果 输入例子:…
#include<bits/stdc++.h> using namespace std; #define maxn 300 #define ll long long ll a,b; ]; inline ll calc(int i){ '; } int main(){ int T; cin>>T; ;tt<=T;tt++){ cin>>s>>b; b=abs(b); a=; int len=strlen(s); ; ]==; for(i;i<len…
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 ou…
1243. Divorce of the Seven Dwarfs Time limit: 1.0 second Memory limit: 64 MB After the Snow White with her bridegroom had left the house of the seven dwarfs, their peaceful and prosperous life has come to an end. Each dwarf blames others to be the re…
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以直接用杨辉三角递推,边做加法边取模. (2) ,   ,并且是素数 本文针对该取值范围较大又不太大的情况(2)进行讨论. 这个问题可以使用Lucas定理,定理描述: 其中 这样将组合数的求解分解为小问题的乘积,下面考虑计算C(ni, mi) %p. 已知C(n, m) mod p = n!/(m!(…
A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the…
题目链接: Segment Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description       Silen August does not like to talk with others.She like to find some interesting problems. Today she finds an interesting pro…
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space. Output For each tes…
当基础数据类型长度无法满足需求时可以使用大数类 构造方法接受字符串为参数 BigInteger bInt = new BigInteger("123123"); BigDecimal bDouble = new BigDecimal("123123.123123124"); 基础操作(取模使用divideAndRemainder方法,返回的数组第二个元素为余数): BigDecimal在做除法时必须设定传入精度(保留多少位小数),否则会出现异常:java.lang.…
题面 输入只有5位,所以转化为long long类型用快速幂取模 前面补0的写法printf("%05lld\n",ans);如果ans不足5位会在前面补0 #include<bits/stdc++.h> using namespace std; long long mod_exp(long long a, long long b, long long c) //快速幂取余a^b%c { long long res, t; res = % c; t = a % c; whi…
Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 93    Accepted Submission(s): 25 Problem Description 度度熊近期非常喜欢玩游戏.这一天他在纸上画了一个2行N列的长方形格子. 他想把1到2N这些数依次放进去.可是为了使格子看起来优美,他想找到使每行每列都递增的方案.只是画了…