版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coldfresh/article/details/82497979
题目来源: Spoj
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
,给出N,求满足条件的整数解的数量。例如:N = 2,1/2 = 1/3 + 1/6,1/2 = 1/4 + 1/4。由于数量可能很大,输出Mod 10^9 + 7。
Input
输入一个数N(1 <= N <= 1000000)。
Output
输出解的数量Mod 10^9 + 7。
Input示例
2
Output示例
2
推导:
唯一分解一下,就做完了。
代码;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxx 1000050
#define ll long long
#define mod 1000000007
using namespace std;
bool isP[maxx];
int prime[maxx],cnt;
void init()
{
for(int i=2;i<maxx;i++)
{
if(!isP[i])prime[cnt++]=i;
for(int j=0;j<cnt&&(ll)i*prime[j]<maxx;j++)
{
isP[i*prime[j]]=true;
if(i%prime[j]==0)break;
}
}
}
int e[maxx];
ll P(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int main()
{
init();
int n;
cin>>n;
if(n==1)
{
cout<<1<<endl;
return 0;
}
int tot=0;
while(prime[tot]<=n)
{
int _n=n;
int p=prime[tot];
while(_n)
{
_n/=p;
e[tot]+=_n;
}
++tot;
}
ll ans=1;
for(int i=0;i<tot;i++)
ans=ans*((ll)e[i]*2+1)%mod;
ans=(ans-1+mod)%mod;
cout<<(ans*P(2,mod-2)%mod+1)%mod<<endl;
return 0;
}