本题亦是非常裸的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;

}

最新文章

  1. Eplan简单教程
  2. Win7快捷方式图标不显示解决办法
  3. 夺命雷公狗---DEDECMS----1dedecms的安装过程
  4. urllib3 PoolManager
  5. Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
  6. hadoop filesystem 删除文件 复制文件 重命名文件
  7. keil or c51 汇编调用c语言函数 容易忽视的问题
  8. HttpServletResponse HttpServletRequest RequestDispatcher
  9. apt-get 总结
  10. 新年上班第一天,我的 IDE 挂了
  11. WeakHashMap回收时机结合JVM 虚拟机GC的一些理解
  12. Windows平台监听服务无法启动报报TNS-12560 TNS-00530案例
  13. maven_问题
  14. DOM LEVEL 1 中的那些事儿[总结篇-上]
  15. C#调用免费天气预报WebService
  16. 【百度】大型网站的HTTPS实践(二)——HTTPS加密算法介绍
  17. Shader 学习工具篇 可视化公式工具ZGrapher
  18. [BZOJ5338][TJOI2018]xor(可持久化Trie)
  19. JavaScript中call,apply,bind方法
  20. Python-urllib学习记录

热门文章

  1. VUE中关于表单提交的简单实现
  2. Daily Scrum 9
  3. 第八次作业——项目UML设计
  4. 活学活用wxPython
  5. python urllib使用
  6. C# 开发者最经常犯的 8 个错误
  7. sql sever 数据表
  8. SonarQube安装
  9. dwarf是怎样处理的栈帧?
  10. html5 download all in one