感谢 EnofTaiPeople 与 Sgt_Dante 教我怎么做。
先把原式转化。,。
(想不明白的话展开一下就懂了)
把两式结合起来,我们有 , 于是问题转换为求 ,设其为 。
设 为满足条件的 ,由于 不能超过 ,且平方必须为 的倍数,则 , 。其中 和 是将 分解质因数的结果,发现其他 必须是 的倍数,设 则有,。
显然 等于 的个数,即 的个数,即 。
于是我们只要把 质因数分解就好了,但数据太大不能直接分解。
我们发现,有 。则 ,所以我们肯定可以求出较小的那一个,注意到 以内的质因子都被我们找到了,则剩下的质因子不超过两个,只要判断最后剩下的 是否是完全平方数就好了。
#include<bits/stdc++.h>
#define pb push_back
#define ls x<<1
#define rs x<<1|1
#define vt vector<int>
#define ll long long
#define pp pair<int,int>
#define N 1000010
#define ull unsigned long long
using namespace std;
const int Maxn=(1<<20);
const ll Minn=1e3;
const int M=1e9+7;
const int inf=1e9;
const ll INF=1e18;
void add(ll &x,ll y) { if((x+=y)>=M) x-=M; }
void add(ll &x,ll y,ll z) { x=(x+y*z%M)%M; }
ll qm(ll x,ll y=M-2) {
ll res=1;
for(;y;x=x*x,y/=2) if(y&1) res=res*x;
return res;
}
int gcd(int x,int y) { return (y==0)?x:gcd(y,x%y); }
ll p[N+10],m,vis[N+10],n;
void init() { //求质数
for(ll i=2;i<=N;i++) {
if(!vis[i]) p[++m]=i;
for(int j=1;j<=m&&p[j]*i<=N;j++) {
vis[p[j]*i]=1;
if(i%p[j]==0) break;
}
}
}
void solve() {
cin>>n; ll a=1,nn=n,ct=0,b=1;
for(int i=1;i<=m;i++) {
ct=0;
while(n%p[i]==0) n/=p[i],ct++; //质因数分解
b=b*qm(p[i],ct+((ct+1)/2)*2-2*ct);ct-=(ct+1)/2;a=a*qm(p[i],ct); //按公式计算a和b
} ll nnn=sqrt(n);
if(nnn*nnn==n) { //是平方数就给a
a%=M;a=a*nnn%M;nn%=M;add(a,nn*nn%M);
printf("%lld\n",a);
}else{ //不是就给b
b=b*n;a=(nn/b);a=sqrt(a);
nn%=M;a%=M;add(a,nn*nn%M);
printf("%lld\n",a);
}
}
signed main() {
ios::sync_with_stdio(false);init();
cin.tie(0);cout.tie(0);int t=1;cin>>t;
while(t--) { solve(); }
return 0;
}