POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)
放一个写的不错的博客: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(拓展中国剩余定理)的更多相关文章
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)
题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
随机推荐
- 2020年国外PhD申请QQ群907928541
2020年申请国外读博的 可以加QQ群:907928541 供大家学习交流套磁!
- MVC5+EF6 入门完整教程5 :UI的一些改造
https://www.cnblogs.com/miro/p/4095165.html 上篇文章介绍了EF实现CRUD及一些基本的Html Helpers. 这次我们将会对之前的内容进行一些修改和重构 ...
- openresty + lua
lua语法总结: 1. 导入模块(用require来加载,模块是一个类型为table的全局变量) 导入写好的包,举例如下: 2. lua 数据类型(有8种:nil.boolean.number.str ...
- php设计模式之观察者模式实例代码
php提供的两个接口,一个被观察者接口SplSubject,一个或多个观察者接口SPLObserver,和一个可以储存对象的类SplObjectStorage.被观察者有三个方法,需要实现这三个方法, ...
- 线程池(ExecutorService)初体验
背景:查询月统计数据,因为查询日统计数据功能已经实现.月统计数据,只是参一个List(date) 参数,for循环调用日统计,然后把结果整合就OK. 问题:单线程跑 太耗时间 解决方案:使用多线程, ...
- 【vue】 router.beforeEach
import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...
- pycharm开发flask指定ip、端口无效
原因分析 是因为使用了pycharm的版本的问题.并不是flask框架本身的问题(不管你是如何设置的flask配置,通过加载config也好,还是通过run的时候传入形参也好,均不影响) 可以很明显的 ...
- Diskpart手动创建EFI、MSR引导分区
当给电脑加新硬盘时候,并且需要把新硬盘当作系统盘,有几种方法来为新硬盘创建引导分区: 可以选择用U盘刻录微软原版系统镜像,直接从U盘启动,然后直接创建分区,直接装,会自动为硬盘创建好几个引导分区. 进 ...
- docker添加potainer可视化管理工具
具体来说就以下几个步骤,一般来说docker的运行环境都是在Linux下,即便是docker desktop装在windows下,默认的环境也是linux 1.先拉去镜像(网络不好的需要挂vpn或者设 ...
- c语言thread用法记录。
https://blog.csdn.net/hitwengqi/article/details/8015646 先是c++11之前的 1.最基础,进程同时创建5个线程,各自调用同一个函数 #inclu ...