poj 2891 Strange Way to Express Integers(一元线性同余方程组)
来源:互联网 发布:孤岛危机2配置优化 编辑:程序博客网 时间:2024/05/14 01:43
迭代求解方程组,拿板子就好了
#include <stdio.h>typedef long long LL;const int N = 11110;LL M[N], R[N];LL gcd(LL a, LL b){ return !b ? a : gcd(b, a%b);}LL extgcd(LL a, LL b, LL &x, LL &y){ LL d = a; if(b) { d = extgcd(b, a%b, y, x); y -= (a/b) * x; } else x = 1, y = 0; return d;}LL linear_congruence(LL M[], LL R[], LL n){ LL m = M[1], r = R[1]; LL x, y, flag = 1; for(LL i = 2; i <= n; i++) { LL d = gcd(m, M[i]), c = R[i] - r; if(c % d != 0) { flag = 0; break; } extgcd(m/d, M[i]/d, x, y); LL tm = M[i] / d; x = ((c/d * x) % tm + tm) % tm; r = r + x*m; m = m/d * M[i]; r %= m; } if(flag) return r?r:r+m; else return -1;}int main(){ LL m; while(scanf("%I64d",&m) != EOF) { for(LL i = 1; i <= m; ++i) scanf("%I64d %I64d",&M[i],&R[i]); printf("%I64d\n",linear_congruence(M, R, m)); } return 0;}
阅读全文
0 0
- POJ 2891 Strange Way to Express Integers(一元线性同余方程组模版题)
- poj 2891 Strange Way to Express Integers(一元线性同余方程组)
- POJ 2891 Strange Way to Express Integers(解一元线性同余方程组)
- POJ 2891 Strange Way to Express Integers 解一元线性同余方程组
- POJ 2891 Strange Way to Express Integers(exgcd—解一元线性同余方程组)
- POJ 2891 Strange Way to Express Integers(一元线性同余方程组)
- poj 2891 Strange Way to Express Integers 一元线性同余方程组
- poj 2891 Strange Way to Express Integers(解线性同余方程组) + hdu 1573
- POJ2891——Strange Way to Express Integers(一元线性同余方程组)
- poj 2891 Strange Way To Express Integers(线性同余方程组)
- poj 2891 Strange Way to Express Integers(线性同余方程组求解)
- POJ 2891 Strange Way to Express Integers解线性同余方程组(中国剩余定理不互质版)
- POJ2891 Strange Way to Express Integers【一元线性同余方程组】
- POJ2891 Strange Way to Express Integers 一元线性同余方程组
- 【POJ2891】Strange Way to Express Integers-解一元线性同余方程组
- poj2891 Strange Way to Express Integers【解一元线性同余方程组模板】
- Strange Way to Express Integers(poj2891线性同余方程组)
- POJ2891 Strange Way to Express Integers 线性同余方程组
- JAVASE 一切都是对象
- 赋值转换
- java多线程详解
- opencv中的Linear SVM例子
- NYOJ 523 亡命逃窜 (bfs)
- poj 2891 Strange Way to Express Integers(一元线性同余方程组)
- 秒杀系统架构分析与实战
- jsp/servlet第四章第一节Jsp九大内置对象及四大作用域概述
- 软件工程(C编码实践篇)学习总结
- Vue 增删示例
- 网站关键词布局的最佳位置,知道5个轻松做排名
- Codeforces 888B Buggy Robot (模拟)
- 【架构性能分析(1)】MySQL性能瓶颈排查定位
- LeetCode 之