Esspe-Peasee 解方程求最大的x整数解

来源:互联网 发布:淘宝网食用菌种植方法 编辑:程序博客网 时间:2024/05/03 06:35
Esspe-Peasee


Time Limit: 10000ms

Memory Limit: 131072KB


This problem will be judged on UVALive. Original ID: 6170
64-bit integer IO format: %lld Java class name: Main

Prev Submit Status Statistics Discuss Next

Font Size: + -


[PDF Link]

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!

Source

Regionals 2012, South Pacific

代码:

#include <iostream>#include <stdio.h>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int M=100005;long long x,y;long long a,b,c;long long solve(long long a,long long  b,long long &x,long long  &y){    if(b==0)    {        x=1,y=0;        return a;    }    long long g=solve(b,a%b,x,y);    long long t=x;    x=y;    y=t-(a/b)*y;    return g;}int main(){    while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF)    {        if(!a && !b && !c)break;        long long g=solve(a,b,x,y);        if(c % g)        {            printf("Unquibable!\n");            continue;        }        x=((x%(b/g)*((c/g)%(b/g)))%(b/g)+(b/g))%(b/g);        y=(c-a*x)/b;        if(y<0)        {           printf("Unquibable!\n");           continue;        }        if(x==1)        printf("1 foom and ");        else        printf("%lld fooms and ",x);        if(y==1)        printf("1 foob for a twob!\n");        else        printf("%lld foobs for a twob!\n",y);    }    return 0;}


原创粉丝点击