LightOJ 1319 - Monkey Tradition CRT除数互质版
2024-09-22 09:28:41
本题亦是非常裸的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;
}
最新文章
- Eplan简单教程
- Win7快捷方式图标不显示解决办法
- 夺命雷公狗---DEDECMS----1dedecms的安装过程
- urllib3 PoolManager
- Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
- hadoop filesystem 删除文件 复制文件 重命名文件
- keil or c51 汇编调用c语言函数 容易忽视的问题
- HttpServletResponse HttpServletRequest RequestDispatcher
- apt-get 总结
- 新年上班第一天,我的 IDE 挂了
- WeakHashMap回收时机结合JVM 虚拟机GC的一些理解
- Windows平台监听服务无法启动报报TNS-12560 TNS-00530案例
- maven_问题
- DOM LEVEL 1 中的那些事儿[总结篇-上]
- C#调用免费天气预报WebService
- 【百度】大型网站的HTTPS实践(二)——HTTPS加密算法介绍
- Shader 学习工具篇 可视化公式工具ZGrapher
- [BZOJ5338][TJOI2018]xor(可持久化Trie)
- JavaScript中call,apply,bind方法
- Python-urllib学习记录