湖南省第十一届大学生计算机程序设计竞赛(阶乘除法)

来源:互联网 发布:便宜又好看的淘宝店铺 编辑:程序博客网 时间:2024/05/17 05:02

问题 F: 阶乘除法

时间限制: 5 Sec  内存限制: 128 MB
提交: 84  解决: 19
[提交][状态][讨论版]

题目描述

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

输入

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

输出

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

样例输入

120

1

210

样例输出

Case 1: 5 1
Case 2: Impossible
Case 3: 7 4
#include<stdio.h>#include<iostream> #include <algorithm>#include<string.h>#include<math.h>#include<queue>#include<set>#define LL long long#define INf 0x3f3f3f3fusing namespace std;int main(){    int n;    int flag,count=1,c,c1;    while(scanf("%d",&n)!=EOF)    {        flag=0;        printf("Case %d: ",count);        count++;        if(n==1)        {            printf("Impossible\n");            continue;        }        else        {            for(int i=2;i*(i-1)<=n;i++)//确保i的阶乘绝对小于等于n            {                if(n%i==0)                {                    int ans=1;                    for(int j=i;j>=1;j--)                    {                        ans=ans*j;                        if(ans==n)                        {                            c=i;                            c1=j-1;                            flag=1;                            break;                        }                    }                }                if(flag)                    break;            }        }        if(flag)        {             printf("%lld %lld\n",c,c1);          }        else            printf("%lld %lld\n",n,n-1);    }    return 0;}


阅读全文
0 0
原创粉丝点击