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,源向每一天连上[ ...
随机推荐
- HBase备份恢复练习
一.冷备 1.创建测试表并插入测试数据 [root@weekend05 ~]# hbase shell hbase(main):005:0> create 'scores','grade','c ...
- input标签属性
很多时候,我们都用到了很多标签实现输入功能,所以在这里梳理一下. 1.建立一个文本框 <input type="text" name="userName" ...
- Lumia 刷机(强刷)Message send failed解决办法
强刷可以救砖,不需要验证地区code,可以跨刷其它国家/地区的固件,但不是所有机型都可以这样,Lumia 620是支持跨刷的. 看本文你首先要知道使用Nokia Care Suite强刷的步骤,参考从 ...
- 【学习笔记】深入理解js原型和闭包系列学习笔记——精华
深入理解js原型和闭包笔记: 1.“一切皆是对象”,对象是属性的集合. 丨 函数也是对象,但是使用typeof时为什么函数返回function而 丨 不是object呢,js为何要对函数做这样的区分 ...
- 使用windows的fsutil命令创建指定大小及类型的测试文件
在软件测试中,对于上传.下载一类功能常常需要用不同大小的文件进行测试. 使用Windows命令fsutil可以生成任意大小.任意类型文件. C:\Users\axia\fsutil file crea ...
- javaee 第四周作业
分析hello.java.下载链接:https://github.com/javaee/tutorial-examples/tree/master/web/jsf/hello1 /** * Copyr ...
- SQLite概述
SQLite概述 这个教程帮助您理解SQLite是什么,它如何不同于SQL,为什么它是必要的和它的方式处理应用程序数据库. SQLite是一个库,实现了一个独立的软件,serverless zer ...
- Android Studio 中文件查询方法总结
搜索单词 Windows: Ctrl + F Mac : Cmd + F 会在当前激活的文件上查询输入的关键字,以高亮显示 跳转行 Windows: Ctrl + L Mac : Cmd + ...
- (一) Docker in Docker
一. 背景介绍 工作中,要实现在docker中运行docker,实现镜像的拉取,创建,修改,上传等操作. 尝试过在docker中,安装docker.行不通,服务起不来. 而且直接在 docker 容 ...
- Java中类,对象,方法的内存分配
Java中类,对象,方法的内存分配 以下针对引用数据类型: 在内存中,类是静态的概念,它存在于内存中的CodeSegment中. 当我们使用new关键字生成对象时,JVM根据类的代码,去堆内存中开辟一 ...