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

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值