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. eclipse下如何关联android-support-v4.jar源码

    一.首先导入jar包 如果android-support-v4.jar包在libs目录下,先将它移除.然后点选中项目右键 --->properties-->javabuildpath--& ...

  2. 初涉Node.js

    Node.js的是建立在Chrome的JavaScript的运行时,可方便地构建快速,可扩展的网络应用程序的平台. Node.js使用事件驱动.非阻塞I/ O模型,是轻量级.高效.完美的跨分布式设备运 ...

  3. 特征值分解,奇异值分解(SVD)

    特征值分解和奇异值分解在机器学习领域都是属于满地可见的方法.两者有着很紧密的关系,我在接下来会谈到,特征值分解和奇异值分解的目的都是一样,就是提取出一个矩阵最重要的特征. 1. 特征值: 如果说一个向 ...

  4. 2140: 稳定婚姻 - BZOJ

    Description 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就 ...

  5. 2565: 最长双回文串 - BZOJ

    Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同). 输入长度为n的串S,求S的最长双回文子串T ...

  6. The service ‘xxx’ configured for WCF is not registered with the Autofac container

    最近在使用autofac.wcf时,报如下异常: Exception Details: System.InvalidOperationException: The service 'xxx' conf ...

  7. 导入 github 步骤

    https://github.com/dotnet/corefx       如果出现未能找到解决方案的情况,则找项目文件打开,如:  

  8. linux杀掉80端口线程命令

    80端口被其他程序占用, fuser -k -n tcp 80

  9. 使用EF code first和asp.net mvc4遇到的问题总结

    最近使用EF code first和asp.net mvc4做项目,遇到些问题,记录一下. 一.EF code first 生成外键列问题. 一般情况下,都是先写一个int型外键id属性,然后写一个外 ...

  10. zoj 2777 Visible Lattice Points(欧拉函数,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<algo ...