输入两个正整数 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<=10^9)。
Output对于每组数据,输出两个正整数 n 和 m。无解输出"Impossible",多解时应让 n 尽量小。
Sample Input120 1 210Sample Output
Case 1: 5 1 Case 2: Impossible
Case 3: 7 4
【分析】
由上式可见,其实此题就是求m+1~n这些连续整数的乘积为k的(n,m)值
注明该题,代码提示来源:http://blog.csdn.net/disparity_CJK/article/details/52437142代码如下:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
ll k;
int t=1;
while(scanf("%lld",&k)!=EOF)
{
printf("Case %d: ",t++);
if(k==1)
{
printf("Impossible\n");
continue;
}
ll num =1;
ll ex_x=0;
ll ey_y=0;
ll i,j;
for( i=1; i*i<=k; i++)
{
num = 1;
for( j= i+1; num<k; j++)
{
num*=j;
}
if(num==k)
{
ex_x = j-1;
ey_y = i;
break;
}
}
///表示该数未找到,那么他就是else的两个数表示。
if(ex_x!=0)
printf("%lld %lld\n",ex_x,ey_y);
else
printf("%lld %lld\n",k,k-1);
}
return 0;
}