51nod 1079 中国剩余定理模板
中国剩余定理就是同余方程组除数为质数的特殊情况
我直接用同余方程组解了。
记得exgcd后x要更新
还有先更新b1再更新m1,顺序不能错!!(不然会影响到b1的更新)
#include<cstdio>
#include<cctype>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
void exgcd(ll a, ll b, ll& d, ll& x, ll& y)
{
if(!b) { d = a; x = 1; y = 0; }
else { exgcd(b, a % b, d, y, x); y -= x * (a / b); }
}
void read(ll& x)
{
ll f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-1') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
}
int main()
{
ll n, m1, b1, m2, b2;
read(n); read(m1); read(b1);
_for(i, 2, n)
{
read(m2); read(b2);
ll A = m1, B = m2, K = b2 - b1, d, x, y;
exgcd(A, B, d, x, y);
x = (x * (K / d) % (B / d) + (B / d)) % (B / d);
b1 = m1 * x + b1;
m1 = m1 / d * m2;
}
printf("%lld\n", b1);
return 0;
}
51nod 1079 中国剩余定理模板的更多相关文章
- 51Nod 1079 中国剩余定理 Label:数论
一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % 2 = 1, K % 3 = 2, K % 5 = 3.符合条件的最小的K = 23. Input 第1行:1个数 ...
- 中国剩余定理模板 51nod 1079
题目链接:传送门 推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误). 1079 中国剩余定理 基准时间限制:1 秒 空间 ...
- 51 Nod 1079 中国剩余定理(孙子定理)NOTE:互质情况
1079 中国剩余定理 一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % 2 = 1, K % 3 = 2, K % 5 = 3.符合条件的最小的K = 23. 收起 ...
- Monkey Tradition---LightOj1319(中国剩余定理模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- [洛谷P1495] 曹冲养猪 (中国剩余定理模板)
中国剩余定理(朴素的)用来解线性同余方程组: x≡a[1] (mod m[1]) x≡a[2] (mod m[2]) ...... x≡a[n] (mod m[n]) 定义ms=m[1]*m[2]*. ...
- 中国剩余定理模板&俄罗斯乘法
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b){d=a;x=1LL;y=0LL;} else {ex_gcd(b,a%b,d, ...
- 中国剩余定理模板poj1006
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #i ...
随机推荐
- node——request和response的常用对象
request(http.IncomingMessage)和response(http.ServerResponse)对象介绍 request:服务器解析用户提交的http请求报文,将结果解析到req ...
- [CTSC1999][网络流24题]家园
题目:洛谷P2754. 题目大意:有$n$个空间站,$m$个飞船,每个飞船有各自的停靠站点,并且从第一个停靠站点开始,不断循环.每个飞船有不同的容量(-1为月球,0为地球).每个飞船初始停在第一个停靠 ...
- BlankUtil(判断是否为空和去除多余空格)
package com.drn.core.util; import java.io.Serializable; import java.util.Map; import java.util.Prope ...
- python dns 服务器
import socketserver import struct import threading # DNS Query class SinDNSQuery: def __init__(self, ...
- koa,express,node 通用方法连接MySQL
这个教程不管node,express,koa都可以用下面方法连接,这里用koa做个参考 这个教程的源码地址: https://github.com/xiaqijian/... 新建文件目录,我是这样子 ...
- Hibernate类没有找到序列化器解决方案
Hibernate类没有找到序列化器解决方案 异常信息类似如下 No serializer found for class org.hibernate.proxy.pojo.javassist.Jav ...
- ActiveMQ_Windows和Linux版本的安装部署
1, 保证电脑上安装了jdk6以上版本的java,并配置了好环境变量 : 2, 官方下载地址:http://activemq.apache.org/download-archives.html ,这里 ...
- angular-代码段
重复代码 <div ng-app="" ng-init="names=['Jani','Hege','Kai']"> <p>使用 ng- ...
- Leetcode--easy系列4
#58 Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space char ...
- 开源TT框架上的日志类
public class Logger { /** * log tag */ private String tagName = "MoGuLogger";// tag name / ...