zoukankan      html  css  js  c++  java
  • 阶乘除法 CSU

    阶乘除法

     CSU - 1781

    输入两个正整数 n, m,输出 n!/m!,其中阶乘定义为 n!= 1*2*3*...*n (n>=1)。 比如,若 n=6, m=3,则n!/m!=6!/3!=720/6=120。

    是不是很简单?现在让我们把问题反过来:输入 k=n!/m!,找到这样的整数二元组(n,m) (n>m>=1)。

    如果答案不唯一,n 应该尽量小。比如,若 k=120,输出应该是 n=5, m=1,而不是 n=6, m=3,因为5!/1!=6!/3!=120,而 5<6。

    Input

    输入包含不超过 100 组数据。每组数据包含一个整数 k (1<=k<=10^9)。

    Output

    对于每组数据,输出两个正整数 n 和 m。无解输出"Impossible",多解时应让 n 尽量小。

    Sample Input

    120
    1
    210

    Sample Output

    Case 1: 5 1
    Case 2: Impossible
    Case 3: 7 4

    Hint

    wa了好多发,脑子不太好使

    #include<stdio.h>
    #include<stdlib.h>
    #include<algorithm>
    #include<iostream>
    #include<math.h>
    #define LL long long
    using namespace std;
    int main()
    {
        LL n;
        int c=0;
        while(scanf("%lld",&n)!=EOF)
        {
            printf("Case %d: ",++c);
            if(n==1)
                printf("Impossible
    ");
                else
            if(n&1)
            {
                printf("%lld %lld
    ",n,n-1);
            }
            else
            {
                int flag=0;
                //int c=sqrt(n*1.0);
                LL ans1,ans2;
                LL i,j;
                for(i=2;i*i<=n;i++)
                {
                    LL temp=i;
                    for(j=i+1;;j++)
                    {
                        temp*=j;
                        if(temp==n)
                        {
                            flag=1;
                            ans1=i;
                            ans2=j;
                            break;
                        }
                        if(temp>n)
                            break;
                    }
                    if(flag)
                        break;
                   // printf("%I64d
    ",i);
                }
                if(flag)
                {
                    printf("%lld %lld
    ",ans2,ans1-1);
                }
                else
                    printf("%lld %lld
    ",n,n-1);
            }
        }
    }
  • 相关阅读:
    mysql创建账号
    CentOS 6.4下Squid代理服务器的安装与配置
    匿名函数 闭包
    array_values打破原有的数组key值,重新从0往上排序
    鹏哥yh搜索
    mosh的安装和使用
    提高php效率的技巧
    svn服务器配置
    限制input输入两位小数
    小程序弹出层覆盖不了canvas
  • 原文地址:https://www.cnblogs.com/upstart/p/8982275.html
Copyright © 2011-2022 走看看