E. Madoka and The Best University(数论&gcd)
Posted Harris-H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E. Madoka and The Best University(数论&gcd)相关的知识,希望对你有一定的参考价值。
E. Madoka and The Best University(数论&gcd)
n
−
c
=
a
+
b
d
=
g
c
d
(
a
,
b
)
n
−
c
d
=
a
d
+
b
d
g
c
d
(
a
d
,
b
d
)
=
1
令
s
=
n
−
c
d
,
u
=
a
d
,
v
=
b
d
s
=
u
+
v
g
c
d
(
s
,
u
)
=
g
c
d
(
u
+
v
,
u
)
=
1
n-c=a+b \\\\ d = gcd(a,b) \\\\ \\dfracn-cd=\\dfracad+\\dfracbd \\\\ gcd(\\dfracad,\\dfracbd)=1 \\\\ 令s=\\dfracn-cd,u=\\dfracad,v=\\dfracbd \\\\ s = u+v\\\\ gcd(s,u)=gcd(u+v,u)=1
n−c=a+bd=gcd(a,b)dn−c=da+dbgcd(da,db)=1令s=dn−c,u=da,v=dbs=u+vgcd(s,u)=gcd(u+v,u)=1
因此我们只需枚举
c
c
c,然后
n
−
c
n-c
n−c的因子为
d
d
d时,计算与
s
s
s互质的个数,即
φ
(
s
)
\\varphi(s)
φ(s)
时间复杂度: O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)
#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;
也可以枚举 d d d,显然 n − c = a + b n-c=a+b n−c=a+b是 d d d的倍数,第二维就枚举 a + b a+b a+b。
时间复杂度: O ( n log 2 n ) O(n\\log^2n) O(nlog2n)
#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"D. Madoka and The Corruption Scheme(组合数学)
D. Madoka and The Corruption Scheme(组合数学)
The Best Technique To Approach The Best Packers And Movers In Bangalore