1781: 阶乘除法
Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 710 Solved: 206Description
输入两个正整数 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
Source
湖南省第十一届大学生计算机程序设计竞赛#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long n;
int count=0;
while(scanf("%lld",&n)!=EOF)
{
count++;
int i,j;
long long s;
int b=0;
int flag=0;
if(n==1)
{
printf("Case %d: Impossible\n",count);
continue;
}
for( i=1; i*i<n&&!flag; i++)
{
int m=n;
for(j=i+1; j<=n; j++)
{
if(m%j)
{
break;
}
else
{
m=m/j;
}
}
if(m==1)
{
printf("Case %d: %d %d\n",count,j-1,i);
flag=1;
}
}
if(flag==0)
{
printf("Case %d: %d %d\n",count,n,n-1);
}
}
return 0;
}