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
k一定是一段连续的数列之积,暴力枚举3ms过
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#define INF 0x3f3f3f3f
#define MAXN 10000005
#define Mod 1000000007
using namespace std;
int main()
{
int cnt=1;
int k;
int tmp=1,a,b;
while(~scanf("%d",&k))
{
int flag=0;
for(int i=2;i<=k;++i)
{
flag=0;
tmp=1;
a=i;
int g=i;
while(k%g==0)
{
tmp*=g;
g++;
if(tmp==k)
{
flag=1;
b=g-1;
break;
}
if(tmp>k)
break;
}
if(flag)
break;
}
if(flag)
printf("Case %d: %d %d\n",cnt++,b,a-1);
else
printf("Case %d: Impossible\n",cnt++);
}
return 0;
}