UVA10548+10717

来源:互联网 发布:苹果6网络通信出现问题 编辑:程序博客网 时间:2024/06/03 22:50

In early days the concept of money was not there, and people used to sell goods by exchanging othergoods. Assume that in a society there are two kinds of goods A and B. Buyer has to pay an amountequivalent to c > 0. If he has the option of giving the equivalent using both type A and B, their unitvalues are respectively a > 0, b > 0. Whereas if one of a and b is negative it is assumed that seller hasthe corresponding item for exchange. At most one of the a and b will be negative. Now the problem isto find the number of ways the exchanges can be done for the buyer for c > 0 if it is possible to do so.

Input

The first line contains an integer n (0 < n < 1001) indicating the number of cases to be considered.Each of the next n lines contains integers a, b and c. You can assume that |a|, |b|, |c| < 231 and none ofa, b or c would be zero.

Output

For each case, if there are a number of combinations in which exchanges for c can be made using goodsA and B, number of such combinations will be printed. In case it is impossible to make such changesthe line will contain the phrase ‘Impossible’. In case infinite numbers of combinations are there, theline will contain the phrase ‘Infinitely many solutions’.

Sample Input

3

3 5 17

7 -23 571 

10 36 7

Sample Output

1

Infinitely many solutions

Impossible

题意:给你三种货物a,b,c,用a,b组成c,判断是否有无限解或者无解

思路:ax+by=c   拓展欧几里得

拓展欧几里得的各点都在这里了,感觉这次才真正理解了这个算法,

为确定有多少个解,确定t的上下限即可

t的确定根据x=x1-x2,得到x2=x1-x,又因为x=(c*x1)/gcd并且x1-x2=(t*b)/gcd得出

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<cmath>using namespace std;#define ll long long#define INF 0x3f3f3f3f3f3fll a,b,c;ll exgcd(ll a,ll b,ll &x,ll &y){    if(!b)    {        x=1;        y=0;        return a;    }    ll d=exgcd(b,a%b,y,x);    y-=a/b*x;    return d;}ll solve(){    ll x,y;    ll gcd=exgcd(a,b,x,y);    //cout<<"gcd="<<gcd<<" x="<<x<<" y="<<y<<endl;    if(c%gcd)        return 0;    ll down=-INF,up=INF;    if (b/gcd>0)    down=max(down,(ll)ceil((-x*1.0*c)/b));    else    up=min(up,(ll)floor((-x*1.0*c)/b));    if(a/gcd>0)    up=min(up,(ll)floor((y*1.0*c)/a));    else    down=max(down,(ll)ceil((y*1.0*c)/a));    if(up==INF||down==-INF)    return -1;    if(down<=up)    {        //cout<<"up="<<up<<" down="<<down<<endl;        return up-down+1;    }    return 0;}int main(){    int t;    cin>>t;    while(t--)    {        cin>>a>>b>>c;        ll ans=solve();        if(ans==0)        cout<<"Impossible"<<endl;        else if(ans<0)        cout<<"Infinitely many solutions"<<endl;        else        cout<<ans<<endl;    }    return 0;}

The Royal Canadian Mint has commissioned a new seriesof designer coffee tables, with legs that are constructedfrom stacks of coins. Each table has four legs,each of which uses a different type of coin. For example,one leg might be a stack of quarters, anothernickels, another loonies, and another twonies. Each legmust be exactly the same length.

Many coins are available for these tables, includingforeign and special commemorative coins. Given an inventoryof available coins and a desired table height,compute the lengths nearest to the desired height forwhich four legs of equal length may be constructed usinga different coin for each leg.

Input

Input consists of several test cases. Each case beginswith an integers: 4 ≤ n ≤ 50 giving the number oftypes of coins available, and 1 ≤ t ≤ 10 giving thenumber of tables to be designed. n lines follow; eachgives the thickness of a coin in hundredths of millimetres.t lines follow; each gives the height of a table tobe designed (also in hundredths of millimetres). A linecontaining ‘0 0’ follows the last test case.

Output

For each table, output a line with two integers: the greatest leg length not exceeding the desired length,and the smallest leg length not less than the desired length.

Sample Input

4 2

50

100

200

400

1000

2000

0 0

Sample Output

800 1200

2000 2000

题意:给n个硬币,m张桌子,n行硬币厚度,m行桌子腿厚度,问由硬币组成桌子腿可以怎么组成 输出两个尽量接近桌子高度的

思路:求多个数的lcm

尽量接近桌子腿长度,一个比他小的最大的,比他大的最小的即可

import java.util.Scanner;public class Main {static int n;static int m;static long min,max;static int [] map1=new int [51];static int [] map2=new int [11];public static int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}public static int lcm(int a,int b){return a*b/gcd(a,b);}public static void solve(int q){for( int i=1;i<=n-3;i++)    {        for(int j=i+1;j<=n-2;j++)        {            int L1=lcm(map1[i],map1[j]);            for(int k=j+1;k<=n-1;k++)            {                int L2=lcm(map1[k],L1);                for(int m=k+1;m<=n;m++)                {                    int L3=lcm(L2,map1[m]);                    int x=L3*(int)Math.floor(1.0*q/L3);                    int y=L3*(int)Math.ceil(1.0*q/L3);                    if(x>min)                    {                        min=x;                    }                    if(y<max)                    {                        max=y;                    }                }            }        }    }}public static void main(String[] args) {// TODO Auto-generated method stubScanner cin=new Scanner(System.in);while(cin.hasNext()){n=cin.nextInt();m=cin.nextInt();if(n==0&&m==0)break;//System.out.println(gcd(n,m)+"  "+lcm(n,m));for(int i=1;i<=n;i++){map1[i]=cin.nextInt();}for(int i=1;i<=m;i++){map2[i]=cin.nextInt();min=-1;max=1000000000;solve(map2[i]);System.out.println(min+" "+max);}}}}