题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> using namespace s…
Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1943    Accepted Submission(s): 693 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins s…
中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647…
<题目链接> 题目大意: 给你一些模数和余数,让你求出满足这些要求的最小的数的值. 解题分析: 中国剩余定理(模数不一定互质)模板题 #include<stdio.h> using namespace std; #define ll long long ll A[],B[];//B[i]为余数 ll dg,ans;//dg为A[i]的最小公倍数 ans 为最小解 void exgcd(ll a, ll b, ll &d, ll&x, ll &y) { ; y…
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的数的个数,可以看成若干个同余方程联立成的一次同余方程组.然后我们就可以很自然而然的想到了中国剩余定理.需要注意的是,在处理中国剩余定理的过程中,可能会发生超出LongLong的情况,需要写个类似于快速幂的快速乘法来处理. 吐槽:赛场上不会快速乘,导致疯狂WA,唉,还是太年轻 代码: #include…
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body an…
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4439    Accepted Submission(s): 1435 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0],…
题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define l…
不互质的中国剩余定理…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=3579 #include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<iomanip>;}…
Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一…
好久没写什么数论,同余之类的东西了. 昨天第一次用了剩余定理解题,今天上百度搜了一下hdu中国剩余定理.于是就发现了这个题目. 题目的意思很简单.就是告诉你n个m[i],和n个a[i].表示一个数对m[i]取模的值为a[i]. 乍一看以为这个题目可以用中国剩余定理,但是看仔细了吗?这里的mi并没有说是互素的哦. 肿么办?只能用安叔以前说的解同余方程的那种土方法了. 上代码:(我稍微改进了一下,根据题目的具体情况). #include <iostream> #include <cstdio…
Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4206    Accepted Submission(s): 1616 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins…
http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For…
<题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的模板题,需要用到Gale_Shapley算法,GS算法讲解  >>> 这个算法还是很直观的. #include <iostream> #include <cstring> #include <stack> #include <string> #…
题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog.csdn.net/hrn1216/article/details/51534607     (其中的图解很详细)   根据图解理解下面代码 #include<cstdio> #include <string> #include<cstring> #include<i…
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include<string.h> #define N 1100 int c[N][N],visit[N][N],n; int number(int x) { return x&-x; } void insert(int x,int y,int z) { int i,j; for(i=x;i<=N;…
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-programmer.com/posts/19363.html 这个博客举例说的挺好的:http://blog.csdn.net/mishifangxiangdefeng/article/details/7109217 hdu 3579 Hello Kiki 中国剩余定理(不互质的情况) 对互质的情况…
HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3107    Accepted Submission(s): 1157 Problem Description One day I was shopping in the supermarket. There was a cashier counti…
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 247 Accepted Submission(s): 107   Problem Description One day I was shopping in the supermarket. There was a cashier counting coins serio…
P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1}b[j]$ ,$ res$是前$ i-1 $个方程的最小解 则$ res+x*M$ 是前 $i-1 $个方程的通解 那么我们求的就是 $res+x*M ≡ a[i] (mod b[i])$ $<=> x*M - y*b[i] = a[i]-res$ 用exgcd求出的解为 t (当且仅当 gcd…
一.除法取模逆元 如果我们要通过一个前面取过模的式子递推出其他要取模的式子,而递推式里又存在除法 那么一个很尴尬的事情出现了,假如a[i-1]=100%31=7 a[i]=(a[i-1]/2)%31 a[i]=50%31=19 ,但我们现在只知道a[i-1]=7,如何计算出a[i]=19呢? a[i]=(7/2)%31=3? 其实本来是100是整除2的,但是对31取模后就不能整除了,所以我们要求出在mod 31意义下2的逆元是多少 口算可得,2*16%31=1,所以2的逆元就是16,所以a[i]…
一.求解模线性方程 由ax=b(mod n) 可知ax = ny + b 就相当于ax + ny = b 由扩展欧几里得算法可知有解条件为gcd(a, n)整除d 可以直接套用扩展欧几里得算法 最终由d个不同解时在模n下有d个不同的数字 二.中国剩余定理 证明可看:https://www.cnblogs.com/MashiroSky/p/5918158.html ll extgcd(ll a, ll b, ll& x, ll& y) //求解ax+by=gcd(a, b) //返回值为gc…
Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 139500   Accepted: 44772 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显的中国剩余定理+容斥,容斥的时候每次要加上个(%7=0)这一组. 中间会爆longlong,所以在其中加上个快速乘法(类似快速幂).因为普通的a*b是直接a个b相加,很可能会爆.但是你可以将b拆分为二进制来加a,这样又快又可以防爆. //#pragma comment(linker, "/STACK…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数的个数. 题目思路: [中国剩余定理][容斥原理][快速乘法][数论] 因为都是素数所以两两互素,满足中国剩余定理的条件. 把7加到素数中,a=0,这样就变成解n+1个同余方程的通解(最小解).之后算L~R中有多少解. 但是由于中国剩余定理的条件是同时成立的,而题目是或的关系,所以要用容斥原理叠加删…
题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a1,(X+d)%28=a2,(X+d)%33=a3,给定a1,a2,a3,d,求最小的X. 题目思路: [中国剩余定理] 23,28,33互素,可以套中国剩余定理. 也可以直接手算逆元. 33×28×a模23的逆元为8,则33×28×8=5544: 23×33×b模28的逆元为19,则23×33×1…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[n] mod 95041567. 分析:首先了解三个概念:贝尔数   第二类斯特灵数   中国剩余定理 贝尔数是指基数为n的集合的划分方法的数目. 贝尔数适合递推公式: 每个贝尔数都是"第二类Stirling数"的和 贝尔数满足两个公式:(p为质数)             1) B[n+…
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_3 \cdots p_k$ 所有$p$的值不相同且p都是质数 求$C(n, m) \ \%\  (p_1*p_2*p_3* \cdots *p_k)$的值 范围:$1\leq m\leq n\leq 1e18,\ 1\leq k\leq 10,p_i\leq 1e5$,保证$p_1*p_2*p_3*…
题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.....,pk)取余. 解题思路: Lucas + 中国剩余定理,注意的是中国剩余定理的时候有可能会爆long long.然后用一个快速加法就好辣. #include <cstdio> #include <cstring> #include <iostream> #inclu…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, 1≤k≤10.1≤m≤n≤10^18. 分析: 如果M是素数,则可以直接用lucas定理来做,但是M不是素数,而是素数的连乘积.令C(n, m)为 X ,则可以利用lucas定理分别计算出 X%p1,X%p2, ... , X % pk的值,然后用中国剩余定理来组合得到所求结果. 比较坑的地方是,…