中国剩余定理模数不互质的情况(poj 2891
中国剩余定理模数不互质的情况
主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小
https://vjudge.net/problem/POJ-2891

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2019.8.8
using namespace std;
long long T,n;
long long r[N],a[N],x,y,gcd,flag;
void in(long long &x){
long long y=;char c=getchar();x=;
while(c<''||c>''){if(c=='-')y=-;c=getchar();}
while(c<=''&&c>=''){ x=(x<<)+(x<<)+c-'';c=getchar();}
x*=y;
}
void o(long long x){
if(x<){p('-');x=-x;}
if(x>)o(x/);
p(x%+'');
} void exgcd(long long a,long long b,long long &x,long long &y){
if(!b){
x=;y=;gcd=a;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
} signed main(){
while(scanf("%lld",&n)!=EOF){
flag=;
For(i,,n)
in(r[i]),in(a[i]);
For(i,,n){
exgcd(r[],r[i],x,y);
if((a[i]-a[])%gcd==){
x*=(a[i]-a[])/gcd;
y=r[i]/gcd;
x=(x%y+y)%y;
a[]+=r[]*x;
r[]=r[]*r[i]/gcd;
}
else{
o(-);p('\n');
flag=;
break;
}
}
if(!flag)
o(a[]%r[1]),p('\n');
}
return ;
}
中国剩余定理模数不互质的情况(poj 2891的更多相关文章
- POJ 2891 中国剩余定理的非互质形式
中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...
- Hello Kiki(中国剩余定理——不互质的情况)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- poj 2891 模数不互质的中国剩余定理
Strange Way to Express Integers Description Elina is reading a book written by Rujia Liu, which intr ...
- HDU3579Hello Kiki(中国剩余定理)(不互质的情况)
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...
- 中国剩余定理模数互质的情况模板(poj1006
http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue& ...
- Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...
- X问题(中国剩余定理+不互质版应用)hdu1573
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- P4777 【模板】扩展中国剩余定理(EXCRT)
思路 中国剩余定理解决的是这样的问题 求x满足 \[ \begin{matrix}x \equiv a_1(mod\ m_1)\\x\equiv a_2(mod\ m_2)\\ \dots\\x\eq ...
- LUOGU P4777 【模板】扩展中国剩余定理(EXCRT)
传送门 解题思路 扩展 $crt$,就是中国剩余定理在模数不互质的情况下,首先对于方程 $\begin{cases} x\equiv a_1\mod m_1\\x\equiv a_2\m ...
随机推荐
- day27-面向对象进阶
#!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...
- jquery操作html元素之(添加元素)
添加新的 HTML 内容 我们将学习用于添加新内容的四个 jQuery 方法: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在 ...
- nodejs中命令行和node交互模式的区分
来自:廖雪峰教程 么么哒~ 命令行模式和Node交互模式 请注意区分命令行模式和Node交互模式. 看到类似C:\>是在Windows提供的命令行模式: 在命令行模式下,可以执行node进入No ...
- scala实现读取Oracle数据
用scala实现读取oracle数据 增加oralce的jar包后 package cn.bigdata.scala.oracle import java.sql.{DriverManager, Co ...
- jq 出现 $.cookie is not a function
出现这种问题有3中可能 1是没有导入cookie相关的库文件 2是cookie的库文件和jquery库文件的先后顺序问题, 必须先导入jquery库文件后到其他库文件 3一个页面由于二次或多次引入了j ...
- sklearn中模型抽取
特征抽取sklearn.feature_extraction 模块提供了从原始数据如文本,图像等众抽取能够被机器学习算法直接处理的特征向量. 1.特征抽取方法之 Loading Features fr ...
- 使用R语言 SDK调取tushare数据
安装Tushare 打开RStudio,在控制台输入命令: > install.packages('Tushare') Tushare的R包需要依赖httr.tidyverse.forecast ...
- USACO2007 捕牛记 /// queue+桶 oj1503
题目大意: John和牛都站在一条直线上,开始时John位于坐标点N上( 0 ≤ N ≤ 100,000 ),牛位于坐标点K上( 0 ≤ K ≤ 100,000 ). John有两种行动方式:步行和瞬 ...
- flink流的执行大致流程图
- MySQL - 锁等待超时与information_schema的三个表
引用地址:https://blog.csdn.net/J080624/article/details/80596958 回顾一下生产中的一次MySQL异常,Cause: java.sql.SQLExc ...