题意:求使不定方程1 / x + 1 / y = 1 / n成立的正整数解。
思路:x、y一定是大于n的数,设y=n+k,则1/x=1/n-1/(n+k),整理一下即x=n^2/k+n,所以只要k是n^2的因子,
就一定有解,那么就转化成了n^2的因子个数。直接求n^2的因子个数容易超时,转化成n来做,
若n=a1^x1+a2^x2+a3^x3+……(a1,a2,a3……都是素因子)则n^2的因子个数为(1+2x1)(1+2x2)(1+2x3)……
(n的因子的个数为(1+x1)(1+x2)(1+x3)……)
最后不要忘记加1除2。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e6+5;
bool temp[N];
int data[N/10];
int cnt=0;
void prime()
{
temp[2]=true;
for(int i=3;i<N;i+=2)
{
temp[i]=true;
temp[i+1]=false;
}
for(int i=3;i<N;i+=2)
if(temp[i])
for(int j=i+i;j<N;j+=i) temp[j]=false;
for(int i=2;i<N;i++)
{
if(temp[i]) data[cnt++]=i;
}
}
ll fun(ll a)
{
ll ans=1;
for(int i=0;i<cnt&&data[i]*data[i]<=a;i++)
{
int c=0;
while(a%data[i]==0)
{
c++;
a/=data[i];
}
ans*=(2*c+1);
}
if(a>1) ans*=3;
ans=(ans+1)/2;
return ans;
}
int main()
{
prime();
int t;
scanf("%d",&t);
int k=0;
while(t--)
{
k++;
ll n;
scanf("%lld",&n);
printf("Scenario #%d:\n%lld\n\n",k,fun(n));
}
return 0;
}