首先,让我们来推导一下这个神奇的式子:
其中 ,而 是满足 的最小正整数。
利用 ,可知 在模 意义下存在乘法阶,记为 ,则有:。
观察 的结构,我们只关心 ,也就是 的奇偶性。设:,则有:。
于是原式可写为:
展开:
第一项为等比数列:
记:
于是:
考虑递推关系:
两边模 ,由于 为奇数,有:,即 。
而 正是计算 的二进制长除法中每一位的商,因此序列 正是 的二进制循环节。
于是有:
所以原式:
整理得:
直接计算 显然不可行,但可以在模 下计算 ,再减一;
利用 ,可以先在整数意义下整除 ,再对 取模;
嗯,貌似就通过一个很巧妙的方式写出来了……
还需计算 :
将 分解为:。
这里用 Pollard’s Rho;利用乘法阶在互质模数下的性质:
取所有结果的 即为 。
#include<bits/stdc++.h>
#define ull unsigned long long
#define u12 __uint128_t
using namespace std;
const ull mod=1000000007ULL;
mt19937_64 rg(chrono::steady_clock::now().time_since_epoch().count());//rand可能会有一些问题
ull poww(ull a,ull b,ull m){
ull r=1%m;
a%=m;
while(b){
if(b&1)r=(ull)((u12)r*a%m);
a=(ull)((u12)a*a%m);
b>>=1;
}
return r;
}
//Miller-Rabin判质数
bool isp(ull n){
if(n<2)return 0;
//先试除小数
for(ull i=2;i<=37;i++){if(n%i==0)return n==i;}
//n-1=d*2^s
ull d=n-1,s=0;
while((d&1)==0){d>>=1;s++;}
//这组底数可以保证ull范围内判质数正确
ull a[7]={2,325,9375,28178,450775,9780504,1795265022};
for(int i=0;i<7;i++){
ull x=a[i]%n;
if(x==0)continue;
x=poww(x,d,n);
if(x==1||x==n-1)continue;
bool ok=0;
for(ull j=1;j<s;j++){
x=(ull)((u12)x*x%n);
if(x==n-1){ok=1;break;}
}
if(!ok)return 0;
}
return 1;
}
void fac(ull n,vector<ull>&v){
vector<ull> st;
st.push_back(n);
//避免递归层数问题
while(!st.empty()){
n=st.back();
st.pop_back();
if(n==1)continue;
if(isp(n)){v.push_back(n);continue;}
ull d=0;
//先处理小质因子
if(n%2==0)d=2;
else if(n%3==0)d=3;
else if(n%5==0)d=5;
else{
//Brent版Pollard-Rho,找n的一个非平凡因子d
while(!d){
ull c=1+rg()%(n-1);
ull y=1+rg()%(n-1);
ull m=128,g=1,r=1,q=1,x=0,z=0;
while(g==1){
x=y;
//先走r步
for(ull i=0;i<r;i++) y=(ull)(((u12)y*y+c)%n);
ull k=0;
while(k<r&&g==1){
z=y;
q=1;
ull lm=min(m,r-k);
//批量累乘
for(ull i=0;i<lm;i++){
y=(ull)(((u12)y*y+c)%n);
ull e=x>y?x-y:y-x;
if(e==0)e=n;
q=(ull)((u12)q*e%n);
}
ull aa=q,bb=n;
while(bb){
ull cc=aa%bb;
aa=bb;
bb=cc;
}
g=aa;
k+=lm;
}
if(r>(1ULL<<62))break;
r<<=1;
}
//如果g=n,说明批量乘坏了,退回逐步找
if(g==n){
g=1;
while(g==1){
z=(ull)(((u12)z*z+c)%n);
ull e=x>z?x-z:z-x;
ull aa=e,bb=n;
while(bb){
ull cc=aa%bb;
aa=bb;
bb=cc;
}
g=aa;
}
}
if(g>1&&g<n)d=g;
}
}
st.push_back(d);
st.push_back(n/d);
}
return ;
}
int main(){
ull n;
cin>>n;
vector<ull> fn;
fac(n,fn);
sort(fn.begin(),fn.end());
ull fi=n;//fi就是指phi
for(int i=0;i<(int)fn.size();i++){
if(i==0||fn[i]!=fn[i-1]){
ull p=fn[i];
fi=(ull)((u12)(fi/p)*(p-1));
}
}
//分解phi(n)
vector<ull> fp;
fac(fi,fp);
sort(fp.begin(),fp.end());
//去重后的phi(n)质因子
vector<ull> fs;
for(int i=0;i<(int)fp.size();i++){
if(i==0||fp[i]!=fp[i-1])fs.push_back(fp[i]);
}
//尝试除质因子
ull m=fi;
for(int i=0;i<(int)fs.size();i++){
ull p=fs[i];
while(m%p==0&&poww(2,m/p,n)==1)m/=p;
}
u12 mdd=(u12)n*mod;
u12 qa=2%mdd,qr=1%mdd;
ull qb=m;
//乘法不能直接乘!会爆int128
while(qb){
if(qb&1){
//使用二进制加法乘法防爆
u12 aa=qr%mdd,bb=qa,rr=0;
while(bb){
if(bb&1){
if(rr>=mdd-aa)rr=rr-(mdd-aa);
else rr+=aa;
}
if(aa>=mdd-aa)aa=aa-(mdd-aa);
else aa+=aa;
bb>>=1;
}
qr=rr;
}
//同样用加法乘法防爆
u12 aa=qa%mdd,bb=qa,rr=0;
while(bb){
if(bb&1){
if(rr>=mdd-aa)rr=rr-(mdd-aa);
else rr+=aa;
}
if(aa>=mdd-aa)aa=aa-(mdd-aa);
else aa+=aa;
bb>>=1;
}
qa=rr;
qb>>=1;
}
//因为2^m-1一定能被n整除,所以这里能直接除
ull k=(ull)((qr-1)/n);
ull nm=n%mod;
ull ans=(ull)((u12)k*((nm+mod-2)%mod)%mod);
cout<<ans;
return 0;
}