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
题解: 只有1的时候,会出现Impossible;
当输入的数是奇数时,直接输出k k-1,因为,任意个连起来的数相乘都是偶 数;
如果是偶数:则利用m*m<=k;来判断m的大小;
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#define LL __int64
using namespace std;
int main()
{
__int64 m,n,i,j;
__int64 k;
int t=1;
//while(scanf("%I64d",&k)!=EOF)
while(cin>>k)
{
__int64 sum;
if(k==1)
{
printf("Case %d: Impossible\n",t++);
continue;
}
if(k%2==1)
{
printf("Case %d: %I64d %I64d\n",t++,k,k-1);
continue;
}
for(m=1;m*m<=k;m++)
{
sum=1;int f=0;
for(j=m+1;;j++)
{
sum*=j;
if(k==sum)
{
printf("Case %d: %I64d %I64d\n",t++,j,m);f=1;
break;
}
if(sum>k)
break;
}
if(f==1)
break;
}
}
return 0;
}