(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 ...
随机推荐
- 编辑器开发之 Range 范围对象的学习
写在前面: 网上有各种富文本编辑器,微博分享等操作,这些功能非常实用,他们就是使用 range,selection 对象来实现的,这两个对象偏冷门,不涉及编辑器一般用不到,range 对象是对选区的操 ...
- 如何加速golang写业务的开发速度
如何加速golang写业务的开发速度 不要忌讳panic golang写业务代码经常会被吐槽,写业务太慢了,其中最大的吐槽点就是,处理各种error太麻烦了.一个项目中,会有30%或者更多的是在处理e ...
- C#语法——消息,MVVM的核心技术。
在C#中消息有两个指向,一个指向Message,一个指向INotify.这里主要讲INotify. INotify也有人称之为[通知],不管叫消息还是通知,都是一个意思,就是传递信息. 消息的定义 I ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
- 多线程协作wait、notify、notifyAll方法简介理解使用 多线程中篇(十四)
在锁与监视器中有对wait和notify以及notifyAll进行了简单介绍 所有对象都有一个与之关联的锁与监视器 wait和notify以及notifyAll之所以是Object的方法就是因为任何一 ...
- Spring基础系列-Web开发
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996902.html SpringBoot基础系列-web开发 概述 web开发就是集成 ...
- MySQL 笔记整理(19) --为什么我只查一行的语句,也执行这么慢?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 19) --为什么我只查一行的语句,也执行这么慢? 需要说明一下,如果M ...
- [TCP/IP] 网络层-简单查看路由表
使用抓包工具排除网络故障:1.如果一台计算机在网络上发广播包,广播的mac地址是全ff,就有可能堵塞2.使用抓包工具,检测广播包和多播包 网络畅通的条件:数据包有去有回1.路由器使用路由表找到目标网段 ...
- 使用 MSIX 打包 DotNetCore 3.0 客户端程序
如何你希望你的 WPF 程序能够以 Windows 的保护机制保护起来,不被轻易反编译的话,那么这篇文章应该能帮到你. 介绍 MSIX 是微软于去年的 Windows 开发者日峰会 上推出的全新应用打 ...
- WebGL或OpenGL关于模型视图投影变换的设置技巧
目录 1. 具体实例 2. 解决方案 1) Cube.html 2) Cube.js 3) 运行结果 3. 详细讲解 1) 模型变换 2) 视图变换 3) 投影变换 4) 模型视图投影矩阵 4. 存在 ...