E. Madoka and The Best University(数论&gcd)

E. Madoka and The Best University(数论&gcd)


因此我们只需枚举E. Madoka and The Best University(数论&gcd),然后的因子为E. Madoka and The Best University(数论&gcd)时,计算与E. Madoka and The Best University(数论&gcd)互质的个数,即

时间复杂度:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {402653189,805306457,1610612741,998244353};
#define() mst(a,b) memset(a,b,sizeof a)
#define() db double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define VI vector<int>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
template <typename T> //x=max(x,y) x=min(x,y)
void cmx(T &x,T y){
if(x<y) x=y;
}
template <typename T>
void cmn(T &x,T y){
if(x>y) x=y;
}

int phi[N],p[N],cnt;
bitset<N>vis;
vector<int>v[N];
void init(int n){
vis[0] = vis[1] = 1;
rep(i,2,n){
if(!vis[i]) p[++cnt] = i,phi[i] = i-1;
for(int j=1;j<=cnt&&i*p[j]<=n;j++){
vis[i*p[j]] = 1;
if(i%p[j]==0){
phi[i*p[j]] = phi[i] * p[j];
break;
}
phi[i*p[j]] = phi[i] * phi[p[j]];
}
}
for(int i=1;i<N;i++)
for(int j=i;j<N;j+=i) v[j].pb(i);
}
int main(){
init(N-1);
ll s= 0;
int n;scanf("%d",&n);
for(int c=1;c<=n-2;c++)
for(int d:v[n-c]){
s=(s+1LL*c/__gcd(c,d)*d*phi[(n-c)/d]%mod)%mod;
}
printf("%lld\n",s);
return 0;
}

也可以枚举E. Madoka and The Best University(数论&gcd),显然E. Madoka and The Best University(数论&gcd)E. Madoka and The Best University(数论&gcd)的倍数,第二维就枚举E. Madoka and The Best University(数论&gcd)

时间复杂度:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {402653189,805306457,1610612741,998244353};
#define mst(a,b) memset(a,b,sizeof a)
#define db double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define VI vector<int>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
template <typename T> //x=max(x,y) x=min(x,y)
void cmx(T &x,T y){
if(x<y) x=y;
}
template <typename T>
void cmn(T &x,T y){
if(x>y) x=y;
}

int phi[N],p[N],cnt;
bitset<N>vis;
vector<int>v[N];
void init(int n){
vis[0] = vis[1] = 1;
rep(i,2,n){
if(!vis[i]) p[++cnt] = i,phi[i] = i-1;
for(int j=1;j<=cnt&&i*p[j]<=n;j++){
vis[i*p[j]] = 1;
if(i%p[j]==0){
phi[i*p[j]] = phi[i] * p[j];
break;
}
phi[i*p[j]] = phi[i] * phi[p[j]];
}
}
}
int main(){
init(N-1);
ll ans= 0;
int n;scanf("%d",&n);
for(int d=1;d<=n-2;d++)
for(int s=(d<<1);s<n;s+=d){
ans=(ans+1LL*d/__gcd(n-s,d)*(n-s)*phi[s/d]%mod)%mod;
}
printf("%lld\n",ans);
return 0;
}

本题的关键其实就是,

E. Madoka and The Best University(数论&gcd)互质,那么E. Madoka and The Best University(数论&gcd)也互质。