(light oj 1319) Monkey Tradition 中国剩余定理(CRT)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319
In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows: ) There are N monkeys who play this game and there are N bamboos of equal heights. Let the height be L meters.
) Each monkey stands in front of a bamboo and every monkey is assigned a different bamboo.
) When the whistle is blown, the monkeys start climbing the bamboos and they are not allowed to jump to a different bamboo throughout the game.
) Since they are monkeys, they usually climb by jumping. And in each jump, the ith monkey can jump exactly pi meters (pi is a prime). After a while when a monkey finds that he cannot jump because one more jump may get him out of the bamboo, he reports the remaining length ri that he is not able to cover.
) And before the game, each monkey is assigned a distinct pi.
) The monkey, who has the lowest ri, wins. Now, the organizers have found all the information of the game last year, but unluckily they haven't found the height of the bamboo. To be more exact, they know N, all pi and corresponding ri, but not L. So, you came forward and found the task challenging and so, you want to find L, from the given information. Input
Input starts with an integer T (≤ ), denoting the number of test cases. Each case starts with a line containing an integer n ( ≤ n ≤ ). Each of the next n lines contains two integers pi ( < pi < , pi is a prime) and ri ( < ri < pi). All pi will be distinct. Output
For each case, print the case number and the minimum possible value of L that satisfies the above conditions. If there is no solution, print 'Impossible'. Sample Input Output for Sample Input
Case :
Case :
题目大意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底部,后来往上跳,每次跳的距离是pi,最后不能跳到树上面所以最后会有个到顶端的距离ri,求L的最小值;
思路:这是一题典型的中国剩余定理题。
关于中国剩余定理:http://www.cnblogs.com/zhengguiping--9876/p/5319813.html
关于扩展欧几里得定理:http://www.cnblogs.com/zhengguiping--9876/p/5308276.html
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <map>
#include <string>
#include <vector>
#include<iostream>
using namespace std;
#define N 10006
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
LL p[N],r[N];
LL n,sum;
void ex_gcd(LL a, LL b, LL &x, LL &y)
{
if(!b)
{
x = ;
y = ;
return ;
}
ex_gcd(b, a%b, x, y);
LL t = x;
x = y;
y = (t - a/b * y);
if(a*b<)
{
x = -x;
y = -y;
}
}
LL china()
{
LL ans = ;
for(int i = ; i < n; i++)
{
LL x, y;
ex_gcd(sum / p[i], -p[i], x, y);
LL N0 = y * p[i] + ;
ans = (ans + N0 * r[i] + sum) %sum;
}
return (ans + sum) % sum;
}
int main()
{
int T,con=;
scanf("%d",&T);
while(T--)
{
scanf("%lld", &n);
sum = ;
for(int i=; i<n; i++)
{
scanf("%lld %lld", &p[i], &r[i]); sum *= p[i];
} LL x = china(); printf("Case %d: %lld\n",con++,x);
}
return ;
}
(light oj 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 ...
- 中国剩余定理 CRT
中国剩余定理 CRT 正常版本CRT 要解的是一个很容易的东西 \[ \begin{aligned} x\equiv a_1(mod\ m_1)\\ x\equiv a_2(mod\ m_2)\\ . ...
- 中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结
中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结 标签:数学方法--数论 阅读体验:https://zybuluo.com/Junlier/note/1300035 前置浅讲 前 ...
- 中国剩余定理(CRT)及其扩展(EXCRT)详解
问题背景 孙子定理是中国古代求解一次同余式方程组的方法.是数论中一个重要定理.又称中国余数定理.一元线性同余方程组问题最早可见于中国南北朝时期(公元5世纪)的数学著作<孙子算经>卷下第 ...
- 1319 - Monkey Tradition
1319 - Monkey Tradition PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- LightOJ 1319 - Monkey Tradition CRT除数互质版
本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...
- Light OJ 1004 - Monkey Banana Problem(DP)
题目大意: 给你一菱形的数字阵,问从最上面走到最下面所能获得的最大值是多少? #include<cstdio> #include<cstring> #include<io ...
- 扩展GCD 中国剩余定理(CRT) 乘法逆元模版
extend_gcd: 已知 a,b (a>=0,b>=0) 求一组解 (x,y) 使得 (x,y)满足 gcd(a,b) = ax+by 以下代码中d = gcd(a,b).顺便求出gc ...
- 中国剩余定理(CRT)及其拓展(ExCRT)
中国剩余定理 CRT 推导 给定\(n\)个同余方程 \[ \left\{ \begin{aligned} x &\equiv a_1 \pmod{m_1} \\ x &\equiv ...
随机推荐
- Spring Boot 面试,一个问题就干趴下了!
最近栈长面试了不少人,其中不乏说对 Spring Boot 非常熟悉的,然后当我问到一些 Spring Boot 核心功能和原理的时候,没人能说得上来,或者说不到点上,可以说一个问题就问趴下了! 这是 ...
- LINQ 常规实践总结
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续两次OrderBy,后面一个有可能会打乱前面一个的排序顺序,可能与预期不符. 要实现sql中的order by word,nam ...
- 为什么要重写hashcode和equals方法?初级程序员在面试中很少能说清楚。
我在面试 Java初级开发的时候,经常会问:你有没有重写过hashcode方法?不少候选人直接说没写过.我就想,或许真的没写过,于是就再通过一个问题确认:你在用HashMap的时候,键(Key)部分, ...
- expect实现自动交互由浅入深
expect实现自动交互由浅入深 作为运维人员可以通过Shell可以实现简单的控制流功能,如:循环.判断等.但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如telnet ...
- qml demo分析(rssnews-常见新闻布局)
一.效果展示 今儿来分析一篇常见的ui布局,完全使用qml编写,ui交互效果友好,如图1所示,是一个常见的客户端新闻展示效果,左侧是一个列表,右侧是新闻详情. 图1 新闻效果图 二.源码分析 首先先来 ...
- 如何将Azure DevOps中的代码发布到Azure App Service中
标题:如何将Azure DevOps中的代码发布到Azure App Service中 作者:Lamond Lu 背景 最近做了几个项目一直在用Azure DevOps和Azure App Servi ...
- 警惕挂着开源的招牌到处坑蒙拐骗的垃圾项目,比如iBase4J
开源界,本是技术爱好者百花齐放.各显其能的地方.但是,不管什么好东西,到了这块奇葩的土地都能变了味.现在的开源界,真的是鱼龙混杂,有些开源软件,不知道是噱头喊得高,还是star刷得好,竟能凭借一身垃圾 ...
- .NetCore采取JWT方式进行身份认证
验证与授权 Authentication(身份认证) 认证是系统对请求的用户进行身份识别的过程. Authorization (授权) 授权是对认证通过后的用户进行权限分配的过程.授权简单理解就是:识 ...
- 说说我为什么看好Spring Cloud Alibaba
最近对<Spring Cloud Alibaba基础教程>系列的催更比较多,说一下最近的近况:因为打算Spring Boot 2.x一起更新.所以一直在改博客Spring Boot专题页和 ...
- cocos creator主程入门教程(十)—— A*寻路
摘要: 五邑隐侠,本名关健昌,10年游戏生涯,现隐居五邑.本系列文章以TypeScript为介绍语言. 这一篇介绍A*寻路算法.在RPG.SLG.模拟经营类游戏,有需要给角色寻路的需求,一般寻路我们采 ...