阶乘除法
Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format:
Submit
Status
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
由于最大为1e9,我们发现连乘数不超过20,也就是最多二十种情况,从20倒回,而且两个数连乘最多不超过100000,三个数不超过1000,所以倒过来计算sum,如果等于val,打断标记当前循环值,减去余下的位数就行了,超过情况的直接取n,n-1
#include <iostream>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int main()
{
int n;
int c=1;
while(cin>>n)
{
int flag=0;
int a = INF, b;
LL vul = n;
if(n==1)
{
cout<<"Case "<<c++<<": Impossible"<<endl;
continue;
}
for(int k=20; k>=2; k--)
{
int maxn;
if(k>5)
maxn=100;
else if(k==5)
maxn=100;
else if(k==4)
maxn=1000;
else if(k==3)
maxn=1000;
else if(k==2)
maxn=100000;
for(int i = k+1; i <= maxn; i++)
{
LL sum = 1;
for(int j = 0; j < k; j++)
{
sum = sum * (i-j);
//
}
//cout<<sum<<endl;
if(sum == vul)
{
flag = 1;
if(a > i)//取最小序
{
a = i;
b = i-k;
}
}
if(sum > n)
{
// cout<<"IM HERE"<<' '<<c++<<endl;
break;
}//
}
}
if(flag)
cout<<"Case "<<c++<<": "<<a<<' '<<b<<endl;
else
cout<<"Case "<<c++<<": "<<n<<' '<<n-1<<endl;
}
return 0;
}