UVA10673 上下界问题
#include <iostream>
#include<cstdio>
using namespace std;
#define LL long long
LL a,b,m,n,d;
void ex_gcd(LL a,LL b,LL &x,LL &y,LL &d)
{
if(b==){
d=a,x=,y=;
}
else{
ex_gcd(b,a%b,x,y,d);
LL t=x;
x=y,y=t-a/b*y;
}
}
int main()
{
LL T;
cin>>T;
for(int i=;i<T;i++)
{
LL x,y;
cin>>x>>y;
if(x%y==){
cout<<<<' '<<y-<<endl;
}
else{
a=x/y,b=a+;
ex_gcd(a,b,m,n,d);
cout<<m*x<<' '<<n*x<<endl;
}
}
return ;
}
Theorem
For any two integers x and k there exists two more integers p and q such that:
<!--[if !vml]--><!--[endif]-->
Its a fairly easy task to prove this theorem, so wed not ask you to do that. Wed ask for something even easier! Given the values of x and k, youd only need to find integers p and q that satisfies the given equation.
<!--[if !supportEmptyParas]--> <!--[endif]-->
Input
The first line of the input contains an integer, T (1≤T≤1000) that gives you the number of test cases. In each of the following T lines youd be given two positive integers x and k. You can safely assume that x and k will always be less than 108.
Output
For each of the test cases print two integers: p and q in one line. These two integers are to be separated by a single space. If there are multiple pairs of p and q that satisfy the equation, any one would do. But to help us keep our task simple, please make sure that the values, <!--[if !vml]--><!--[endif]--> and <!--[if !vml]--><!--[endif]-->fit in a 64 bit signed integer.
对于这道题目来说,要注意上下界的问题,当x%k==0时,它的上界和下界是一样的,因为答案有多种,输出一个即可,所以此时将答案定位1和k-1即可。
在x%k!=0时,它的上界和下界相差1,那么很自然的想到它们的最大公约数为1,所以可以直接用扩展欧几里德算法。
当然因为x是最大公约数的x倍,所以最后答案要乘上x
代码如下:
UVA10673 上下界问题的更多相关文章
- bzoj 1061 志愿者招募 有上下界费用流做法
把每一天看作一个点,每一天的志愿者数目就是流量限制,从i到i+1连边,上下界就是(A[i],+inf). 对于每一类志愿者,从T[i]+1到S[i]连边,费用为招募一个志愿者的费用,流量为inf.这样 ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
- 【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)
Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s repr ...
- 【BZOJ-4213】贪吃蛇 有上下界的费用流
4213: 贪吃蛇 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 58 Solved: 24[Submit][Status][Discuss] Desc ...
- 【BZOJ-2055】80人环游世界 上下界费用流 (无源无汇最小费用最大流)
2055: 80人环游世界 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 321 Solved: 201[Submit][Status][Discus ...
- HDU 4940 Destroy Transportation system(无源汇有上下界最大流)
看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...
- HDU3157 Crazy Circuits(有源汇流量有上下界网络的最小流)
题目大概给一个电路,电路上有n+2个结点,其中有两个分别是电源和负载,结点们由m个单向的部件相连,每个部件都有最少需要的电流,求使整个电路运转需要的最少电流. 容量网络的构建很容易,建好后就是一个有源 ...
- ZOJ3229 Shoot the Bullet(有源汇流量有上下界网络的最大流)
题目大概说在n天里给m个女孩拍照,每个女孩至少要拍Gi张照片,每一天最多拍Dk张相片且都有Ck个拍照目标,每一个目标拍照的张数要在[Lki, Rki]范围内,问最多能拍几张照片. 源点-天-女孩-汇点 ...
- zoj 3229 Shoot the Bullet(无源汇上下界最大流)
题目:Shoot the Bullet 收藏:http://www.tuicool.com/articles/QRr2Qb 把每一天看成一个点,每个女孩也看成一个点,增加源和汇s.t,源向每一天连上[ ...
随机推荐
- sass+compass起步
前言:Sass is an extension of CSS that adds power and elegance to the basic language. It allows you to ...
- JVM内存各个区域分工简单介绍
JVM内存各个区域简单介绍: 程序计数器:程序计数器是一块较小的内存空间,它可以看作是当前线程所执行的字节码的行号指示器. 在使用多线程时,为了线程切换后能恢复到正确的执行位置,每条线程都需要有个独立 ...
- Dom 获取、Dom动态创建节点
一.Dom获取 1.全称:Document Object Model 文档对象模型 2.我们常用的节点类型 元素(标签)节点.文本节点.属性节点(也就是标签里的属性). 3.docum ...
- BOM学习-javascript计时器小结
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 人物丨让小三吐血,让原配泣血——24K渣男郎咸平
http://url.cn/5swgmythttps://www.toutiao.com/i6650650793743483395人物丨让小三吐血,让原配泣血——24K渣男郎咸平 人物丨让小三吐血,让 ...
- 修改JRE system library
MyEclipse 默认的情况下JRE system library 是:MyEclipse 的,如何修改工程中的JRE system library呢?步骤如下: 1.选择工程->Proper ...
- js里面Object的一些方法
1.Object.freeze() 阻止修改现有属性的特性和值,并阻止添加新属性两种用法:Object.freeze( { } ) 和 Object.freeze( object ) <scri ...
- HDU 5416 CRB and Tree (技巧)
题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...
- python 与
python的与运算是 and &表示的是位运算 c++则是& 和 &&
- 简单DP内容
1. 最长上升子序列 [题目描述] 给定N个数,求这N个数的最长上升子序列的长度. [样例输入] 7 2 5 3 4 1 7 6 [样例输出] 4 第一种解法:时间复杂度O(n^2), 状态设计:DP ...