放一个写的不错的博客: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. spring(三):DefaultListableBeanFactory

  2. 洛谷P1433 吃奶酪

    #include<iostream> #include<math.h> using namespace std ; ; int n; bool st[N]; double x[ ...

  3. Java爬虫学习(2)之用对象保存文件demo(1)

    package com.mieba.spider; import java.util.ArrayList; import java.util.List; import java.util.Vector ...

  4. Python记通用列表操作之切片!

    ______________________________________除使用索引(indexing)来访问单个元素外,还可使用切片 (slicing) 来访问特定范围内的元素. 切片适用于提取序 ...

  5. Java引擎

    import java.io.FileNotFoundException; import java.io.FileReader; import java.util.List; import javax ...

  6. tomcat、nginx、apache、tengine都是什么,及其作用

      Tomcat的功能职责:Tomcat运行在JVM之上,它和HTTP服务器一样,绑定IP地址并监听TCP端口,同时还包含以下指责: • 管理Servlet程序的生命周期• 将URL映射到指定的Ser ...

  7. C#中数据类型char*,const char*和string的三者转换

    C#中数据类型char*,const char*和string的三者转换: . const char* 和string 转换 () const char*转换为 string,直接赋值即可. EX: ...

  8. js实现页面的秒数倒计时

    <button name="vcode_mail" class="btn btn-default" type="button" id= ...

  9. Java“被迫”退出争霸,Python继续霸占首位!老牌程序员:我不服

    2019年转眼已经接近尾声,如果盘点下2019年最火的语言,除了Python还能有谁?你心中的王者语言又是谁? 这一年Python风光无限 这一年JAVA走向右边 这一年,我们都很感慨,你呢? 关于P ...

  10. JSP中四大作用域详解

    四大作用域 为了在页面.请求.和用户之间传递和共享数据,JSP提供了四个不同的作用域:page(页面作用域).request(请求作用域).session(会话作用域).application(应用程 ...