Esspe-Peasee


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的更多相关文章

  1. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  2. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  3. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  4. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  5. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  6. 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 ...

  7. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  8. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. 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 ...

随机推荐

  1. 【转】jquery-取消冒泡

    转自:http://blog.163.com/css_mm/blog/static/209182176201262665157634/ 1.通过返回false来取消默认的行为并阻止事件起泡. jQue ...

  2. 关于拓扑排序(topologicalsort)

    假设我们有一组任务要完成,并且有些任务要在其它任务完成之后才能开始,所以我们必须非常小心这些任务的执行顺序.如果这些任务的执行顺序足够简单的话,我们可以用链表来存储它们,这是一个很好的方案,让我们可以 ...

  3. asp.net asp:TextBox控件绑定值后,获取不到新值问题解决方法

    把Page_Load里绑定的代码放在    if(!IsPostBack){}里面后,即可获取到更新的值. 意思为第一次加载执行.

  4. 【BZOJ】【2561】最小生成树

    网络流/最小割 对于所有小于L的边求一个割使得U,V不连通,这样就可以保证L可能在最小生成树里. 最大生成树同理. 答案累加一下即可.(Orz Hzwer) (我一开始怎么会sb地去想到一起求呢……) ...

  5. hlsl 的tex函数

    texCUBE http://msdn.microsoft.com/en-us/library/windows/desktop/bb509687(v=vs.85).aspx

  6. 个人Android作品开发——FinancePad记账通

    开发背景:针对在外工作的年轻一族,记录平时生活消费记录,方便清楚自己的钱花在哪些地方,方便管理. 开发时间:2013年7月中旬 开发环境:Eclipse Andorid SDK V2.0 开发语言:J ...

  7. poj 3625 Building Roads(最小生成树,二维坐标,基础)

    题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...

  8. C#和Javascript中 正则表达式使用的总结

    说明:本文并非原创,而是从网站上搜集了一些资料整理的!如有雷同,纯属巧合 1.js中正则表达式的使用 在js中定义正则表达式很简单,有两种方式,一种是通过构造函数,一种是通过//,也就是两个斜杠.例如 ...

  9. node.js 安装、图文详解

    网上的教程很多,有些模糊不清,有些版本太旧,有些是.exe安装,本文讲述windows系统下简单nodejs .msi环境配置.最新版是Current version: v0.10.26.官网下载地址 ...

  10. hdu 4579 Random Walk 概率DP

    思路:由于m非常小,只有5.所以用dp[i]表示从位置i出发到达n的期望步数. 那么dp[n] = 0 dp[i] = sigma(dp[i + j] * p (i , i + j)) + 1 .   ...