放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html

POJ好像不能用__int128。

#include <iostream>
#include <stdio.h> typedef long long ll;
const int maxn=1e6+10;
ll m[maxn],r[maxn]; void exgcd(ll a,ll b,ll &x,ll &y)
{
if (b==0) {
x=1;
y=0;
return ;
}
exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-(a/b)*y;
} ll inv(ll a,ll b)
{
ll x,y;
exgcd(a,b,x,y);
while (x<0) {
x+=b;
}
return x;
} ll read()
{
char ch=getchar();
ll x=0,f=1;
while (ch<'0'||ch>'9') {
if (ch=='-') {
f=-1;
}
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return x;
} void print(ll x)
{
if (x<0) {
putchar('-');
x=-x;
}
if (x>9) {
print(x/10);
}
putchar(x%10+'0');
} ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
} int main()
{
ll k;
while (scanf("%lld",&k)!=EOF) {
for (int i=1;i<=k;i++) {
m[i]=read();
r[i]=read();
}
bool flag=0;
for (int i=2;i<=k;i++) {
ll g=gcd(m[i],m[i-1]);
if ((r[i]-r[i-1])%g!=0) {
flag=1;
break;
}
r[i]=(inv(m[i-1]/g,m[i]/g)*(r[i]-r[i-1])/g)%(m[i]/g)*m[i-1]+r[i-1];
m[i]=m[i]*m[i-1]/g;
r[i]=(r[i]%m[i]+m[i])%m[i];
}
if (flag) {
puts("-1");
}
else {
print(r[k]);
puts("");
}
}
return 0;
}

POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)的更多相关文章

  1. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  2. POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)

    题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...

  3. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  4. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  5. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  6. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  7. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  8. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  9. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  10. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

随机推荐

  1. setCascadeOpacityEnabled

    setCascadeOpacityEnabled Enable or disable cascade opacity, if cascade enabled, child nodes' opacity ...

  2. C++-1019-Number Sequence

    题意: 求数字112123123412345123456123456712345678123456789123456789101234567891011123456789101112123456789 ...

  3. 洛谷 1219:八皇后 (位运算 & DFS)

    题目链接: https://www.luogu.org/problem/show?pid=1219#sub row:受上面的皇后通过列控制的位置 ld:受上面的皇后通过从右至左的斜对角线控制的位置 r ...

  4. 洛谷 P4555 [国家集训队]最长双回文串(Manacher)

    题目链接:https://www.luogu.com.cn/problem/P4555 首先明白两个回文串,那么要使两个回文串成立,那么我们只能把$'#'$作为中间节点. 然后我们跑一边Manache ...

  5. Visual Studio 配置 fftw 库

    前提条件: 1.vs 2010 +(我的是2019): 2.下载 fftw. 先将vs 的 msvc 编译器的位置添加到path,一般在下面这个目录下: Microsoft Visual Studio ...

  6. IntelliJ IDEA之如何设置JVM运行参数

    步骤一: 点击IDEA右上角的 Edit Configurations 设置参数: -XX:+PrintGCDetails -Xmx128M -Xms128M 步骤二:在VM options中设置参数 ...

  7. css之变形(transform)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. [Python]python中assert和isinstance的用法

    assert语句是一种插入调试断点到程序的一种便捷的方式. assert == assert == True assert ( == ) print('-----------') assert ( = ...

  9. Bugku-CTF之login1(SKCTF) [SQL约束攻击]

    Day26 login1(SKCTF) http://123.206.31.85:49163/flag格式:SKCTF{xxxxxxxxxxxxxxxxx}hint:SQL约束攻击  本题要点:SQL ...

  10. radar图生成用户guideline

    1.最后生成的效果是这样的: 2.第一个对话框如下(包含了样图),用于输入维度个数[最小为4,最大不限]: 3.第二个对话框如下,根据维度个数生成了信息录入表[每个维度3个信息,每3个一次重复,不清楚 ...