5118: Little Difference
时间限制: 3 Sec 内存限制: 512 MB提交: 130 解决: 31
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
Little Lidia likes playing with numbers. Today she has a positive integer n, and she wants to decompose
it to the product of positive integers.
Because Lidia is little, she likes to play with numbers with little difference. So, all numbers in decomposition should differ by at most one. And of course, the product of all numbers in the decomposition must be equal to n. She considers two decompositions the same if and only if they have the same number of integers and there is a permutation that transforms the first one to the second one.
Write a program that finds all decompositions, which little Lidia can play with today.
Because Lidia is little, she likes to play with numbers with little difference. So, all numbers in decomposition should differ by at most one. And of course, the product of all numbers in the decomposition must be equal to n. She considers two decompositions the same if and only if they have the same number of integers and there is a permutation that transforms the first one to the second one.
Write a program that finds all decompositions, which little Lidia can play with today.
输入
The only line of the input contains a single integer n (1≤n≤1018).
输出
output the number of decompositions of n, or -1 if this number is infinite.
样例输入
12
样例输出
3
题意:给一个n,把n进行分解,分解成任意多个因子相乘的形式,并且要求这些因子之前相差不能超过1,如果这样的因子有无限多个则输出-1,否则就输出方案数。
分析:必须要开三次方枚举,也就是从分成的因子数大于开始三考虑,否则会超时,然后特判一下分成两个因子的情况。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
ll n,ans=0;
int main()
{
ll i,j,temp;
scanf("%lld",&n);
temp=n;
while(temp%2==0)
temp=temp/2;
if(temp==1)
{
printf("-1\n");
return 0;
}
for(i=2;i*i*i<=n;i++)
{
temp=n;
if(temp%i==0)
{
while(temp%i==0)
temp=temp/i;
while(temp%(i+1)==0)
temp=temp/(i+1);
if(temp==1)
ans++;
}
}
ll p=sqrt(n);
if(p*p==n)
ans++;
else if((p+1)*(p+1)==n)
ans++;
else if(p*(p+1)==n)
ans++;
printf("%lld\n",ans+1);
return 0;
}