题目链接: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)的更多相关文章

  1. LightOJ 1319 Monkey Tradition(中国剩余定理)

    题目链接:https://vjudge.net/contest/28079#problem/U 题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n ...

  2. 中国剩余定理 CRT

    中国剩余定理 CRT 正常版本CRT 要解的是一个很容易的东西 \[ \begin{aligned} x\equiv a_1(mod\ m_1)\\ x\equiv a_2(mod\ m_2)\\ . ...

  3. 中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结

    中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结 标签:数学方法--数论 阅读体验:https://zybuluo.com/Junlier/note/1300035 前置浅讲 前 ...

  4. 中国剩余定理(CRT)及其扩展(EXCRT)详解

    问题背景   孙子定理是中国古代求解一次同余式方程组的方法.是数论中一个重要定理.又称中国余数定理.一元线性同余方程组问题最早可见于中国南北朝时期(公元5世纪)的数学著作<孙子算经>卷下第 ...

  5. 1319 - Monkey Tradition

    1319 - Monkey Tradition   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  6. LightOJ 1319 - Monkey Tradition CRT除数互质版

    本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...

  7. Light OJ 1004 - Monkey Banana Problem(DP)

    题目大意: 给你一菱形的数字阵,问从最上面走到最下面所能获得的最大值是多少? #include<cstdio> #include<cstring> #include<io ...

  8. 扩展GCD 中国剩余定理(CRT) 乘法逆元模版

    extend_gcd: 已知 a,b (a>=0,b>=0) 求一组解 (x,y) 使得 (x,y)满足 gcd(a,b) = ax+by 以下代码中d = gcd(a,b).顺便求出gc ...

  9. 中国剩余定理(CRT)及其拓展(ExCRT)

    中国剩余定理 CRT 推导 给定\(n\)个同余方程 \[ \left\{ \begin{aligned} x &\equiv a_1 \pmod{m_1} \\ x &\equiv ...

随机推荐

  1. 2. CMake 系列 - 编译多文件项目

    目录 1. 编译不使用第三方库的项目 1.1 项目目录结构 1.2 相关代码 1.3 编译 2. 编译使用第三方库的项目 2.1 项目目录结构 2.2 相关代码 2.3 编译 1. 编译不使用第三方库 ...

  2. Python基础(reduce,filter,map函数)

    map函数: map函数特点:对可迭代对象中的每个元素进行相同的操作(例如每个元素+1等等) #————————————————map函数———————————————————— #对列表的各个元素实 ...

  3. 使用remix发布部署 发币 智能合约

    Remix是一个基于浏览器的编译器和IDE,使用户能够使用Solidity语言构建以太坊合约并调试事务. 在上一篇文章已经成功的使用代码讲智能合约编译并且发布部署到了链上,可是在部署 发币的智能合约 ...

  4. Java基础知识回顾之五 ----- 多线程

    前言 在上一篇文章中,回顾了Java的集合.而在本篇文章中主要介绍多线程的相关知识.主要介绍的知识点为线程的介绍.多线程的使用.以及在多线程中使用的一些方法. 线程和进程 线程 表示进程中负责程序执行 ...

  5. 【Vue】 ----- 浅谈vue的生命周期

    一.概念 vue生命周期,又叫生命周期钩子函数,是组件从创建到销毁的过程. 二.主要的八大生命周期 1.首先,为方便观察每个周期的特点,我们模拟一个"one"组件的创建与销毁,并在 ...

  6. 痞子衡嵌入式:极易上手的可视化wxPython GUI构建工具(wxFormBuilder)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是wxPython GUI构建工具wxFormBuilder. 一.手工代码布局GUI界面的烦恼 如果你曾经设计过上位机软件GUI界面,初 ...

  7. C# 绘制PDF嵌套表格

    嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用.下面的示例中,将介绍如何通过C#编程来演示如何插入嵌套表格到PDF文档 ...

  8. .net 笔试面试总结(1)

    趁着在放假时候,给大家总结一点笔试面试上的东西,也刚好为年后跳槽做一点小积累. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补缺用的,真正 ...

  9. JavaScript基础-3

    3 运算符 按照个数分类可分为:一元运算符.二元运算符.三元运算符: 按照功能分类可分为:算数运算符.自增运算符.比较运算符.逻辑运算符.赋值运算符: 3.1 算数运算符 算术运算符包含了加减乘除,符 ...

  10. react native中一次错误排查 Error:Error: Duplicate resources

    最近一直在使用react native中,遇到了很多的坑,同时也学习到了一些移动端的开发经验. 今天在做一个打包的测试时,遇到了一个问题,打包过程中报错“Error:Error: Duplicate ...