本题亦是非常裸的CRT。
CRT的余数方程![](http://images2015.cnblogs.com/blog/842069/201610/842069-20161028161934953-1166770635.png)
那么定义![](http://images2015.cnblogs.com/blog/842069/201610/842069-20161028162143593-487259604.png)

其中

为模mi的逆元。

/** @Date    : 2016-10-23-15.11

* @Author : Lweleth (SoungEarlf@gmail.com)

* @Link : https://github.com/Lweleth

* @Version : $

*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

#define LL long long

#define MMF(x) memset((x),0,sizeof(x))

#define MMI(x) memset((x), INF, sizeof(x))

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 1e5+2000;



LL r[16];

LL p[16];

LL gcd(LL a, LL b)

{

return b?gcd(b, a % b):a;

}

LL exgcd(LL a, LL b, LL &x, LL &y)

{

LL d = a;

if(!b)

{

x = 1;

y = 0;

}

else

{

d = exgcd(b , a % b, y, x);

y -= (a / b) * x;

}

return d;

}

LL Inv(LL a, LL b)//exgcd求逆元

{

LL g = gcd(a, b);

if(g != 1)

return -1;

LL x, y;

exgcd(a, b, x, y);

return (x % b + b) % b;

}

//x--= (r1*M1*(M1^-1)+r2*M2*(M2^-1)…rn*Mn*(Mn^-1)) mod M;

//M 是所有互素p的乘积 Mi 是 M/p[i]

//M^-1是 模 p[i]的逆元

LL CRT(LL *r, LL *p, int n)

{

LL M = 1;

LL ans = 0;

for(int i = 0; i < n; i++)

{

M *= p[i];

}

for(int i = 0; i < n; i++)

{

LL x, y;

LL Mi = M / p[i];

ans = (ans + r[i] * Mi * Inv(Mi, p[i])) % M;

}

if(ans < 0)

ans += M;

return ans;

}

int main()

{

int T;

int cnt = 0;

cin >> T;

while(T--)

{

int n;

scanf("%d", &n);

for(int i = 0; i < n; i++)

{

scanf("%lld%lld", p + i, r + i);

}

LL ans = CRT(r, p, n);

printf("Case %d: %lld\n", ++cnt, ans);

}

return 0;

}

LightOJ 1319 - Monkey Tradition CRT除数互质版的更多相关文章

  1. LightOJ 1319 Monkey Tradition(中国剩余定理)

    题目链接:https://vjudge.net/contest/28079#problem/U 题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n ...

  2. 1319 - Monkey Tradition

    1319 - Monkey Tradition   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  3. X问题(中国剩余定理+不互质版应用)hdu1573

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)

    题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...

  5. POJ 2891- Strange Way to Express Integers CRT 除数非互质

    题意:给你余数和除数求x 注意除数不一定互质 思路:不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质 合并互质求余方程 m1x -+ m2y = r2 - r1 先用exgcd求出特 ...

  6. hdu X问题 (中国剩余定理不互质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory ...

  7. [HDU3240]Counting Binary Trees(不互质同余除法)

    Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. C语言:互质

    今天遇到一道奇怪的程序题,和平常的不同.同样都是互质,但是一般的题目都是判断两个数字是否互质,但这道题则是给定一个数字n,要求输出所有小于等于n的与n互质的数,题目已经在下面给出: 质数与互质概念不是 ...

  9. openjudge7834:分成互质组 解析报告

    7834:分成互质组 总时间限制:  1000ms 内存限制:  65536kB 描述 给定n个正整数,将它们分组,使得每组中任意两个数互质.至少要分成多少个组? 输入 第一行是一个正整数n.1 &l ...

随机推荐

  1. 自测之Lesson9:时钟与信号

    题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...

  2. Spring学习(三)—— 自动装配案例分析

    Spring_Autowiring collaborators 在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配).byName,byType,construct ...

  3. mysql中删除重复记录,并保留重复数据中的一条数据的SQL语句

    正好想写一条删除重复语句并保留一条数据的SQL,网上查了一部分资料写的很详细,但还是在这里写下自己的理解,以遍后续学习 .如下: 表字段和数据: SQL语句: [sql] view plain cop ...

  4. 【bzoj1334】[Baltic2008]Elect 背包dp

    题目描述 N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如果某个政党退出后,其它党 ...

  5. hdu 3549 Flow Problem (网络最大流)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  6. 【题解】Atcoder AGC#03 E-Sequential operations on Sequence

    仙题膜拜系列...首先我们可以发现:如果在截取了一段大的区间之后再截取一段小的区间,显然是没有什么用的.所以我们可以将操作序列变成单调递增的序列. 然后怎么考虑呢?启示:不一定要考虑每一个数字出现的次 ...

  7. BZOJ1096:[ZJOI2007]仓库建设——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1096 L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于 ...

  8. The driver has not received any packets from the server

    解决方法: jdbc的url添加参数: jdbc.url=jdbc:mysql://localhost:3306/totosea?useUnicode=true&characterEncodi ...

  9. 从零开始学Linux系统(二)之基本操作指令

    ifconfigping ip地址帮助:ping -t ip地址ping -c 次数 ip地址ping -s 包的大小关机重启:shutdown -h now reboot清屏:clear  == C ...

  10. 【初级算法】2.买卖股票的最佳时机 II

    题目: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必 ...