zoukankan      html  css  js  c++  java
  • Finding LCM (最小公倍数)

    Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

    []   [Go Back]   [Status]  

    Description

    LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, c) = L if and only if L is the least integer which is divisible bya, b and c.

    You will be given a, b and L. You have to find c such that LCM (a, b, c) = L. If there are several solutions, print the one where c is as small as possible. If there is no solution, report so.

    Input

    Input starts with an integer T (≤ 325), denoting the number of test cases.

    Each case starts with a line containing three integers a b L (1 ≤ a, b ≤ 106, 1 ≤ L ≤ 1012).

    Output

    For each case, print the case number and the minimum possible value of c. If no solution is found, print 'impossible'.

    Sample Input

    3

    3 5 30

    209475 6992 77086800

    2 6 10

    Sample Output

    Case 1: 2

    Case 2: 1

    Case 3: impossible

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 using namespace std;
     6 long long gcd(long long x,long long y)
     7 {
     8     if(y==0)return x;
     9     return gcd(y,x%y);
    10 }
    11 int main()
    12 {
    13     long long a,b,l;
    14     int n,i,j;
    15     cin>>n;
    16     for(i=1; i<=n; i++)
    17     {
    18         cin>>a>>b>>l;
    19         a=a/gcd(a,b)*b;
    20         if(l%a)
    21         {
    22             printf("Case %d: ",i);
    23             printf("impossible
    ");
    24         }
    25         else
    26         {
    27            b=l/a;
    28            while(gcd(a,b)!=1)
    29            {
    30                long long t=gcd(a,b);
    31                b*=t;
    32                a/=t;
    33            }
    34           printf("Case %d: ",i);
    35             printf("%lld
    ",b);
    36         }
    37     }
    38 }
    View Code
  • 相关阅读:
    Finder那点事
    mysql基础综述(四)
    【算法编程】过河问题
    CodeForces 7D Palindrome Degree 字符串hash
    语音信号短时域分析之预处理(三)
    sqlite学习笔记7:C语言中使用sqlite之打开数据库
    cpu真实占用率检測工具
    Mediator Design Pattern 中介者模式
    POJ-3984-迷宫问题-BFS(广搜)-手写队列
    HDU 4858 项目管理
  • 原文地址:https://www.cnblogs.com/ERKE/p/3588639.html
Copyright © 2011-2022 走看看