zoukankan      html  css  js  c++  java
  • UVa11889

    11889 Benefit
    Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans, he
    who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B
    equals to C and he will pay back a round bill. Or otherwise take some snack instead of the remaining of
    his money. He believes that finding such a number is hard enough that dissuades students from paying
    that.
    You should write a program that help poor students giving the appropriate amount of money to
    Yaghoub. Of course if there are several answers you go for students’ benefit which is the lowest of them.
    Input
    The first line begin with an integer T (T  100000), the number of tests. Each test that comes in a
    separate line contains two integers A and C (1  A;C  107).
    Output
    Print the lowest integer B such that LCM(A;B) = C in a single line. If no such integer exists, print
    ‘NO SOLUTION’ instead. (Quotes for clarity)
    Sample Input
    32
    6
    32 1760
    7 16
    Sample Output
    3
    55
    NO SOLUTION

    题意:

           输入两个整数A、C,求最小的B使得lcm(A,B)=C。如果无解则输出“NO SOLUTION”。

    分析:

           考虑B=C/A。设G=gcd(A,B)。如果G=1则答案就是B,否则的话考虑一下A和B的素因子唯一分解,A和B的G值一定是它们共同具有的素因子的若干次幂的乘积,我们只需要将数A中具有的这些素因子的幂次全部为B乘上,得到一个新数B’,就是答案。

     1 #include <cstdio>
     2 int gcd(int x,int y){return y == 0 ? x : gcd(y,x % y);}
     3 int main(){
     4     int T; scanf("%d",&T);
     5     while(T--){
     6         int a,c; scanf("%d%d",&a,&c);
     7         if(c % a) printf("NO SOLUTION
    ");
     8         else{
     9             int b = c / a;
    10             int g = gcd(a,b);
    11             if(g == 1){
    12                 printf("%d
    ",b);
    13             }
    14             else{
    15                 int k = 1;
    16                 while(g != 1){
    17                     k *= g;
    18                     a /= g;
    19                     g = gcd(a,b);
    20                 }
    21                 printf("%d
    ",b * k);
    22             }
    23         }
    24     }
    25     return 0;
    26 }
    View Code
  • 相关阅读:
    中文词语的语法划分
    程序员转行可以做什么?
    Linux Crontab 定时任务 命令详解
    Spring对AOP的支持
    ASP.NET 2.0 Web Part编程入门
    linux ramdom hung up
    VLC plugin加载代码分析
    mac上的ssh proxy客户端 iSSH个人修改版
    关于MP4 fileformat中 duration及timescale相关的几个地方
    inline 小结
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5837357.html
Copyright © 2011-2022 走看看