中国剩余定理的应用
2015-11-03 20:56
417 查看
设n>=2,m1,m2,....mn,是两两互质的正整数,记 M = ∏mi, Mi = M/mi.
则同余方程组
X≡a1(mod m1)
X≡a2 (mod m2)
X≡an (mod mn)
有对模M的唯一解
X≡∑aiMiMi’(mod M)
上述就是中国剩余定理
下面给出求此类同余方程组最小非负整数解的代码 LL 代表 long long
View Code
则同余方程组
X≡a1(mod m1)
X≡a2 (mod m2)
X≡an (mod mn)
有对模M的唯一解
X≡∑aiMiMi’(mod M)
上述就是中国剩余定理
下面给出求此类同余方程组最小非负整数解的代码 LL 代表 long long
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <queue> #include <map> #include <algorithm> #include <vector> using namespace std; const int maxn = 1000005; typedef long long LL; LL a[20],m[8],M; LL ex_gcd(LL a,LL b,LL &x,LL &y) { if(b == 0){ x = 1; y = 0; return a; } LL r = ex_gcd(b,a%b,x,y); LL t = x; x = y; y = t - a/b*y; return r; } LL China(LL r) { M = 1; LL i,Mi,x0,y0,d,ans = 0; for(i=1;i<=r;i++){ M *= m[i]; } for(i=1;i<=r;i++){ Mi = M/m[i]; ex_gcd(Mi,m[i],x0,y0); ans = (ans+Mi*x0*a[i])%M; } if(ans < 0) ans += M; return ans; } int main() { int cas = 0; LL p,e,i,d,temp; while(scanf("%lld%lld%lld%lld",&p,&e,&i,&d)!=EOF){ cas++; if(p==-1&&e==-1&&i==-1&&d==-1) break; a[1] = p; a[2] = e; a[3] = i; m[1] = 23; m[2] = 28; m[3] = 33; LL r = 3; LL ans = China(r); while(ans <= d) ans += M; printf("Case %d: the next triple peak occurs in %lld days.\n",cas,ans-d); } return 0; }
View Code
相关文章推荐
- PostgreSQL 9.2.4所有命令的语法帮助及SQL语句简要说明
- Sublime下中文乱码问题
- 详解FCM算法原理及应用
- 迷宫
- DFS 暴力 1007
- 迷茫大三
- 让input支持 ctrl v上传粘贴图片? 让input支持QQ截图或剪切板中的图像数据(Java实现保存)
- 查看程序的启动参数,入口参数
- 观点的不同
- RelativeLayout相对布局
- 使用 jdbc 连接 mysql 数据库
- 数据库中文问题
- 决策树模型组合之随机森林与GBDT
- hdu 1254 推箱子(嵌套搜索,bfs中有dfs)
- swift 学习记录(四)
- 关于NSNotificationCenter监听并且传递数据
- linux下 mysql 忘记密码解决方案
- Codevs_P3269 混合背包
- bitmap算法
- mongo客户端mongo VUE增删改查