(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 ...
随机推荐
- Oracle和Mysql获取uuid的方法对比
场景:orm框架用mybatis,需要往数据库新增一条数据,用Oracle和mysql数据库分别需要怎么实现? mysql方法,用mysql提供的uuid函数 <insert id=" ...
- 【机器学习】--xgboost初始之代码实现分类
一.前述 上节我们讲解了xgboost的基本知识,本节我们通过实例进一步讲解. 二.具体 1.安装 默认可以通过pip安装,若是安装不上可以通过https://www.lfd.uci.edu/~goh ...
- 《前端之路》之三 数组的属性 && 操作方法(下)
咱们 接着上篇来讲- 7.slice() 从某个已有的数组返回选定的元素 经常用来将类数组转化成数组,这样做一方面可以利用现有的数组方法更加方便的处理,另一方面是处于性能的考虑 var f = fun ...
- 使用Maven的assembly插件实现自定义打包
一.背景 最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有 ...
- React的入门知识与概念【1】
回顾在以往的项目开发中,从最初的使用的原生html+js+css+jquery开发,到后来随着项目功能的增加,也渐渐学习了Vue.js框架的开发,以及Vue.js的全家桶Axios,Vue-route ...
- 通过数据分析告诉你北京Python开发的现状
相信各位同学多多少少在拉钩上投过简历,今天突然想了解一下北京Python开发的薪资水平.招聘要求.福利待遇以及公司地理位置.既然要分析那必然是现有数据样本.本文通过爬虫和数据分析为大家展示一下北京Py ...
- Python:游戏:贪吃蛇原理及代码实现
一.游戏介绍 贪吃蛇是个非常简单的游戏,适合练手.先来看一下我的游戏截图: 玩法介绍:回车键:开始游戏空格键:暂停 / 继续↑↓←→方向键 或 WSAD 键:控制移动方向. 食物分红.绿.蓝三种,分别 ...
- SQLServer事务在C#当中的应用
1:事务是什么 事务指的是一系列SQL操作的逻辑工作单元,,要么完全地执行,要么完全地不执行. 一个逻辑工作单元必须有4个属性,原子性(Atomic).一致性(Consistent).隔离型(Isol ...
- 2019/1.7 js面向对象笔记
面向对象 1.构造函数里的属性怎么看?看this,谁前面有this谁就是属性. num不是属性,是私有作用域下的私有变量. 2.如何查找面向对象中的this 1.构造函数的this指向实例对象 2.如 ...
- 策略模式 Strategy 政策Policy 行为型 设计模式(二十五)
策略模式 Strategy 与策略相关的常见词汇有:营销策略.折扣策略.教学策略.记忆策略.学习策略.... “策略”意味着分情况讨论,而不是一概而论 面对不同年龄段的人,面对不同的商品,必然将会 ...