LightOJ 1319 - Monkey Tradition CRT除数互质版
本题亦是非常裸的CRT。
CRT的余数方程
那么定义
则
其中
为模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除数互质版的更多相关文章
- LightOJ 1319 Monkey Tradition(中国剩余定理)
题目链接:https://vjudge.net/contest/28079#problem/U 题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n ...
- 1319 - Monkey Tradition
1319 - Monkey Tradition PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- X问题(中国剩余定理+不互质版应用)hdu1573
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)
题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...
- POJ 2891- Strange Way to Express Integers CRT 除数非互质
题意:给你余数和除数求x 注意除数不一定互质 思路:不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质 合并互质求余方程 m1x -+ m2y = r2 - r1 先用exgcd求出特 ...
- hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- [HDU3240]Counting Binary Trees(不互质同余除法)
Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- C语言:互质
今天遇到一道奇怪的程序题,和平常的不同.同样都是互质,但是一般的题目都是判断两个数字是否互质,但这道题则是给定一个数字n,要求输出所有小于等于n的与n互质的数,题目已经在下面给出: 质数与互质概念不是 ...
- openjudge7834:分成互质组 解析报告
7834:分成互质组 总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个正整数,将它们分组,使得每组中任意两个数互质.至少要分成多少个组? 输入 第一行是一个正整数n.1 &l ...
随机推荐
- 索引值迭代-enumerate
你想在迭代一个序列的同时跟踪正在被处理的元素索引?内置的enumerate() 函数可以很好的解决这个问题: list_c = ['a', 'b', 'c'] for i, c in enumerat ...
- 20162328蔡文琛week01
学号20162328 <程序设计与数据结构>第1周学习总结 教材学习内容总结 通过练习课本上给出的代码并结合老师所提供教程,熟悉并初步了解Java的基本编辑 教材学习中的问题和解决过程 无 ...
- mysql 相同表结构拷贝数据
第一种方法: 在导出表结构的时候可以勾选导出数据: 第二种方法: 表已经存在了,只需要数据即可.这个时候可以编写sql语句(暂不支持不同服务器之间的表数据复制) insert into tab_a(i ...
- DAY3敏捷冲刺
站立式会议 工作安排 (1)服务器配置 (2)数据库配置 燃尽图 燃尽图有误,已重新修改,先贴卡片的界面,后面补修改后燃尽图 代码提交记录
- [solution]xdebug正确配置,但不显示错误信息
一开始以为是配置问题,其实不是,折腾了好久,貌似中文网页很少有人提到这事,更别提解决之道! 最好还是用英文关键词google之:得如下网页 https://bugs.launchpad.net/ubu ...
- ACM 第七天
水题 B - Minimum’s Revenge There is a graph of n vertices which are indexed from 1 to n. For any pair ...
- TCP系列07—连接管理—6、TCP连接管理的状态机
经过前面对TCP连接管理的介绍,我们本小节通过TCP连接管理的状态机来总结一下看看TCP连接的状态变化 一.TCP状态机整体状态转换图(截取自第二版TCPIP详解) 二.TCP连接建立 ...
- MD5加密的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- UML图之协作图
创建方法: 1,new----collaboration diagram 2,根据序列图按F5转换 增加对象链接(图2-4-4) (1) 选择Object Link 工具栏按钮. (2) 单击要 ...
- ifstat查看网络流量的原理
ifstat查看网卡流量的原理:读的是哪个/proc/ 接口啊 同diskIO一样,网络的IO也同样有统计计数的,是/proc/net/dev一个典型的输出就是这个样子的: root@station6 ...