放一个写的不错的博客: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. SpringBoot与动态多数据源切换

      本文简单的介绍一下基于SpringBoot框架动态多数据源切换的实现,采用主从配置的方式,配置master.slave两个数据库. 一.配置主从数据库 spring: datasource: ty ...

  2. L1-7 谁是赢家

    思路 这题好简单,可以分析一下,没有别的情况了. 代码 #include <bits/stdc++.h> using namespace std; int main() { int p1, ...

  3. wireshark抓pc上的包

    简介:wirkshark是全世界最广泛的网络封包分析软件之一. 软件用途: 网络管理员:我用它检测网络问题, 网络安全工程师:我用它检查资讯安全相关问题, 开发者:我用它为新的通讯协定除错, 普通使用 ...

  4. SpringBean的生命周期以及循环依赖过程

    上面就是springBean的大致生命周期. Bean的创建过程 创建Bean之前会调用Bean工厂的后置处理器,可以获取到BeanDefinition Bean的初始化过程 初始化之前会调用前置处理 ...

  5. Codeforces Round #619 (Div. 2)C(构造,容斥)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; int main(){ ios::syn ...

  6. web布局相关

    1.用table布局时,如果设置了table-layout:fixed或者对第一行的两个列进行了合并后导致后面的列宽度失效,这是可以使用 <colgroup>        <col ...

  7. MODULE BUILD FAILED: ERROR: COULDN’T FIND PRESET “ES2015” RELATIVE TO DIRECTORY

    npm run dev 遇到报错: Module build failed: Error: Couldn't find preset "es2015" relative to di ...

  8. Loading class `com.mysql.jdbc.Driver'. This is deprecated

    注意mysql的版本,pom.xml里面的版本.External Librarlies里面的mysql版本.application.properties版本都要检查 有时候还会报 Invalid bo ...

  9. 多表更新:update,join

    1.多表更新: 下面我建两个表,并执行一系列sql语句,仔细观察sql执行后表中数据的变化,很容易就能理解多表联合更新的用法. 前期准备工作: update join_teacher_class jo ...

  10. 转载:Linux 时钟基础学习

    1.HZ Linux核心每隔固定周期会发出timer interrupt (IRQ 0),HZ是用来定义每一秒有几次timer interrupts.举例来说,HZ为1000,代表每秒有1000次ti ...