poj2891
来源:互联网 发布:知天下资源吧网址 编辑:程序博客网 时间:2024/05/22 12:03
难得见中级中的水题。。
一开始做,想着存下来,如下:
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 100000000
LL ext_gcd(LL a, LL b, LL& x, LL& y)
{
LL t, ret;
if (!b)
{
x = 1, y = 0;
return a;
}
ret = ext_gcd(b, a%b, x, y);
t = x, x = y, y = t - a / b*y;
return ret;
}
LL modular_linear_system(LL b [], LL w [], LL k)
{
LL d, x, y, a = 0, m, n = 1, i;
for (i = 0; i < k; i++)
n *= w[i];
for (i = 0; i < k; i++)
{
m = n / w[i];
d = ext_gcd(w[i], m, x, y);
a = (a + y*m*b[i])%n;
}
return (a + n)%n;
}
int main()
{
int t;
while (cin >> t)
{
LL * r = new LL[MAXN];
LL * a = new LL[MAXN];
for (int i = 0; i < t; i++)
{
cin >> r[i] >> a[i];
}
LL ans = modular_linear_system(a, r, t);
cout << ans << endl;
}
}
using namespace std;
#define LL long long
#define MAXN 100000000
LL ext_gcd(LL a, LL b, LL& x, LL& y)
{
LL t, ret;
if (!b)
{
x = 1, y = 0;
return a;
}
ret = ext_gcd(b, a%b, x, y);
t = x, x = y, y = t - a / b*y;
return ret;
}
LL modular_linear_system(LL b [], LL w [], LL k)
{
LL d, x, y, a = 0, m, n = 1, i;
for (i = 0; i < k; i++)
n *= w[i];
for (i = 0; i < k; i++)
{
m = n / w[i];
d = ext_gcd(w[i], m, x, y);
a = (a + y*m*b[i])%n;
}
return (a + n)%n;
}
int main()
{
int t;
while (cin >> t)
{
LL * r = new LL[MAXN];
LL * a = new LL[MAXN];
for (int i = 0; i < t; i++)
{
cin >> r[i] >> a[i];
}
LL ans = modular_linear_system(a, r, t);
cout << ans << endl;
}
}
就和这个主题的名字一样。。too young too naive...各种RE,根本没有告诉k的范围,乱猜哪行哇~
然后改了改,不存,逐个算就是了
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define MAXN 100000000
LL ext_gcd(LL a, LL b, LL& x, LL& y)
{
LL t, ret;
if (!b)
{
x = 1, y = 0;
return a;
}
ret = ext_gcd(b, a%b, x, y);
t = x, x = y, y = t - a / b*y;
return ret;
}
LL modular_linear(LL a, LL b, LL m)
{
LL d, x, y;
d = ext_gcd(a, m, x, y);
if (b % d != 0) return -1;
return (x * (b / d) % m + m) % m;
}
int main()
{
LL a1, a2, m1, m2, k, y;
while (cin>>k)
{
bool flag = false;
cin >> m1 >> a1;
if (k == 1) y = a1;
for (int i = 1; i < k; i++)
{
cin >> m2 >> a2;
if (flag) continue;
LL y = modular_linear(m2, a1 - a2, m1);
if (y == -1) { flag = true; continue; }
a1 = a2 + m2 * y;
m1 = m1*m2 / (__gcd(m1, m2));
a1 = (a1 % m1 + m1) % m1;
}
if (flag) cout << -1 << endl;
else cout << a1 << endl;
}
return 0;
}
#include <algorithm>
using namespace std;
#define LL long long
#define MAXN 100000000
LL ext_gcd(LL a, LL b, LL& x, LL& y)
{
LL t, ret;
if (!b)
{
x = 1, y = 0;
return a;
}
ret = ext_gcd(b, a%b, x, y);
t = x, x = y, y = t - a / b*y;
return ret;
}
LL modular_linear(LL a, LL b, LL m)
{
LL d, x, y;
d = ext_gcd(a, m, x, y);
if (b % d != 0) return -1;
return (x * (b / d) % m + m) % m;
}
int main()
{
LL a1, a2, m1, m2, k, y;
while (cin>>k)
{
bool flag = false;
cin >> m1 >> a1;
if (k == 1) y = a1;
for (int i = 1; i < k; i++)
{
cin >> m2 >> a2;
if (flag) continue;
LL y = modular_linear(m2, a1 - a2, m1);
if (y == -1) { flag = true; continue; }
a1 = a2 + m2 * y;
m1 = m1*m2 / (__gcd(m1, m2));
a1 = (a1 % m1 + m1) % m1;
}
if (flag) cout << -1 << endl;
else cout << a1 << endl;
}
return 0;
}
- poj2891
- poj2891???
- poj2891
- poj2891
- poj2891
- poj2891 中国剩余定理
- POJ2891&&HDU3579&&HDU1573
- poj2891(中国剩余定理)
- POJ2891 Strange Way to Express Integers
- poj2891一次线性模余定理
- poj2891(一元线性同于方程组)
- poj2891 Strange Way to Express Integers
- poj2891 扩展欧几里得解同余方程组
- 中国剩余定理 poj 1006 poj2891
- 【poj2891】 Strange Way to Express Integers
- [POJ2891] Strange Way to Express Integers
- poj2891 Strange Way to Express Integers
- 【模板】【POJ2891】扩展中国剩余定理
- 2013计算机视觉代码合集三
- some win api knowledge
- windows 8 下 TortoiseSVN 图标无法正常显示
- 黑马程序员 ---网络编程 UDP
- 左侧分类菜单的标题修改------Holiday模板
- poj2891
- 聊聊那些令人愉悦的动画特效(GIF图)
- hdu3722Card Game(KM最大带权匹配)
- ubuntu配置从root启动方法
- PHP输出当前进程所有变量 / 常量 / 模块 / 函数 / 类
- CString用法大全
- MIT牛人林达华 图˙谱˙马尔可夫过程˙聚类结构
- Java集合总结
- JAVA反射机制