• 2015湖南省省赛 阶乘除法 暴力


    阶乘除法
    Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:

    Description

    输入两个正整数 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




    一开始写搓了 暴力T了一发 以为不可以 结果就是暴力....

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INF     0x3f3f3f3f
    #define INFLL   0x3f3f3f3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    typedef pair<int,int> PII;
    
    LL k;
    LL ans1;
    LL ans2;
    
    int main()
    {
        //FIN
        int cas = 1;
        while(~scanf("%I64d", &k)){
            if(k == 1){
                printf("Case %d: Impossible
    ", cas++);
                continue;
            }
            LL i, j;
            int flag = 0;
            for(i = 2;i*i <= k; i++)  {
                    LL p = i;
                    for(j = i+1;; j++)  {
                        p *= j;
                        if(p == k)  {
                            flag = 1;
                            ans1 = j;
                            ans2 = i;
                            break;
                        }
                        if(p > k)
                            break;
                    }
                    if(flag)
                        break;
            }
            if(flag)  printf("Case %d: %I64d %I64d
    ", cas++, ans1, ans2-1);
            else  printf("Case %d: %I64d %I64d
    ", cas++, k, k-1);
    
        }
        return 0;
    
    }
    

      

  • 相关阅读:
    图像的卷积
    信息理论与编码中有关信源编码的笔记
    Java 数组排序
    完全平方数
    Java 作业题4
    Java 作业题3
    Java 作业题 2
    算法面试题二:旋转数组,存在重复元素,只出现一次的数字
    算法面试题一:排序算法及贪心算法
    微信小程序 发送模板消息的功能实现
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5774839.html
Copyright © 2020-2023  润新知