uva live 6170
Esspe-Peasee is an ancient game played by children throughout the land of Acmania. The rules are simple:
A player simply quibs the yorba at the kwonk. If the yorba hurms the kwonk the player gets a foom. If the yorba hurfs the kwonk the player gets a foob.
The objective is to get a twob with as few quibs as possible.
Every group of children has its own opinion regarding the value of a foom, the value of a foob, and the value of a twob. However, everyone agrees that a foob is worth more than a foom, and that a twob is worth more than a foob. You may assume that a foom and a foob can each be represented by a 32 bit integer, and a twob can be represented by a 64 bit integer.
Input
You will be given a number of game instances to solve. Each instance is specified by 3 non-negative integers that represent the value of a foom, a foob and a twob, respectively. The final line contains three 0's and should not be processed.
Output
For each instance your program should print `A fooms and B foobs for a twob!', on a line by itself as shown in the samples below, where the value of ``A" fooms plus ``B" foobs add up to a twob, and the sum of ``A" and ``B" is as small as possible. ``fooms" and ``foobs" should be appropriately pluralised, as shown in ``Sample Output" below.
If there is no such pair you should print out the age-old chant: `Unquibable!'
Sample Input
1 6 15
7 9 22
7 9 32
0 9 18
2 5 9
0 0 0
Sample Output
3 fooms and 2 foobs for a twob!
Unquibable!
2 fooms and 2 foobs for a twob!
0 fooms and 2 foobs for a twob!
2 fooms and 1 foob for a twob!
扩展欧几里得算法不再累赘,网上各种大神讲解。orz
顺便总结一下
ax+by=c 若有解,即c%gcd(a,b)==0,以下均为有解情况:
若c=1 && gcd(a,b)==1
特解 (x0 , y0)
通解 (x0+b*t , y0-a*t)
若c==_c*gcd(a,b)
原方程左右同除gcd(a,b)可简化为 _ax+_by=_c gcd(_a,_b)==1 故转化为上述情况 _ax+_by=1
求得特解为 (x0*_c , y0*_c)
故通解为 (x0*_c+_b*t, y0*_c-_a*t) 即 (x0*c/gcd(a,b)+b/gcd(a,b)*t , y0*c/gcd(a,b)-a/gcd(a,b)*t)
这题值得注意而且经常需要用到的地方,就是x,y>0且保证x+y最小
若需x>0 可直接
x=(x*c%b+b)%b即可找出最小的正数x
y=(c-a*x)/b即可求得对应y
若y<0 则不可能出现x,y同时>0的情况,因为x已经是最小的正数,若减小,则x为负,若增大,则y会减小,y为负
#include <cstdio>
#include <cstring>
long long a,b,c,d,x,y; long long exgcd(long long a, long long b, long long &x, long long &y)
{
if(b==)
{
x=;
y=;
return a;
}
long long ret=exgcd(b, a%b, x, y);
long long ty=y;
y=x-a/b*y;
x=ty;
return ret;
} int main()
{
while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF &&(a || b || c))
{
long long d=exgcd(a,b,x,y); if(c%d!=)
printf("Unquibable!\n");
else
{
a=a/d;
b=b/d;
c=c/d;
x=(((x%b)*(c%b)%b)+b)%b;//x刚好大于0 即x的前一个就已经小于0 若使得y小于0 说明无解
y=(c-a*x)/b;
if(y<)
{
printf("Unquibable!\n");
continue;
}
if(x==)
{
printf("1 foom and ");
if(y==) printf("1 foob for a twob!\n");
else printf("%lld foobs for a twob!\n",y);
}else
{
printf("%lld fooms and ",x);
if(y==) printf("1 foob for a twob!\n");
else printf("%lld foobs for a twob!\n",y);
} }
}
return ;
}
uva live 6170的更多相关文章
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
随机推荐
- SQL Server 创建表 添加主键 添加列常用SQL语句【转】
--删除主键alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段名 ...
- 3242: [Noi2013]快餐店 - BZOJ
Description 小T打算在城市C开设一家外送快餐店.送餐到某一个地点的时间与外卖店到该地点之间最短路径长度是成正比的,小T希望快餐店的地址选在离最远的顾客距离最近的地方. 快餐店的顾客分布在城 ...
- map中的erase成员函数用法
转载于 http://www.cnblogs.com/graphics/archive/2010/07/05/1771110.html http://hi.baidu.com/sdkinger/it ...
- linux源代码阅读笔记 linux文件系统(二)
上一篇文章说到linux文件系统中分为超级块,inode块,block块.inode块给出文件的权限,修改时间,大小等信息. 但是实际上,文件的数据是存储在block块中的.而inode块中给出了存储 ...
- poj 1797 Heavy Transportation(最短路变种2,连通图的最小边)
题目 改动见下,请自行画图理解 具体细节也请看下面的代码: 这个花了300多ms #define _CRT_SECURE_NO_WARNINGS #include<string.h> #i ...
- HDU 3501 Calculation 2 (欧拉函数)
题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eula ...
- DJANGO中,用QJUERY的AJAX的json返回中文乱码的解决办法
和网上其它用JAVA或是PHP的实现不太一样, DJANGO中的解决办法如下: 后端样例: def render_to_json_response(context, **response_kwargs ...
- lintcode 中等题:find the missing number 寻找缺失的数
题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 注意 可以改变序 ...
- 什么是spring?
一.对spring的理解. 1.Spring是实现了工厂模式的工厂类(什么是工厂类?简单的来说就是把需要的接口定义到一个类中,需要的时候不用新建,直接从这个类中调用该接口就可以了), 这个类的名字为B ...
- eclipse安装插件的各种方法
做为当下最流行的开源IDE之一,Eclipse的一大优势就在于其无数优秀的插件.一个好的插件可以大大的提高我们的工作效率,学习如何安装Eclipse插件自然也是必修课了.下面介绍Eclipse插件的安 ...