2.20模拟赛T3解析
零.送给未来复习的自己:
如果一段时间后看不懂自己的题解了,可以尝试拿
一.题意:
对于给定的
二.思路:
首先看到
现在转化题目,考虑什么时候满足题意,通过gcd和lcm的形式化定义我们发现,
现在我们考虑
我们思考,上述过程是否可以拓展呢?
显然是可以的,我们对于每一个
考虑优化,我们发现不包含
其中
其中
code for 80pts:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define int long long
inline int read() {
int x=0,f=1;char ch=getchar();
while(ch > '9' || ch < '0'){if(ch == '-'){f = -1;}ch = getchar();}
while(ch >= '0'&&ch <= '9'){x = x * 10 + ch - 48; ch = getchar();}
return x * f;
}
const int MOD = 998244353;
const int PRIME_NUM = 20;
int n,a[PRIME_NUM],cnt,three_shift[PRIME_NUM],ans,bit_3;
void divide(int x){
int sq = std::sqrt(x);
for(int i = 2;i <= sq;++i)
{
if(x % i == 0)
{
int res = 0;
while(x % i == 0) x /= i,++res;
a[++cnt] = res;
}
}
if(x == 1) return ;
a[++cnt] = 1;
}
int qpow(int a,int b)
{
int res = 1;
while(b)
{
if(b & 1) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
void change(int x)
{
for(int i = 1;i <= bit_3;++i) three_shift[i] = 0;
bit_3 = 0;
while(x)
{
three_shift[++bit_3] = x % 3;
x /= 3;
}
}
signed main()
{
n = read();
divide(n);
int up_limit = qpow(3,cnt);
for(int i = 0;i < up_limit;++i)
{
change(i);
int res = 1,cnt_parity = 0,cnt_1 = 0;//cnt_parity判断最终容斥系数奇偶
for(int j = 1;j <= cnt;++j)
{
res = res * (a[j] - three_shift[j] + 1) % MOD;
if(three_shift[j] == 1) ++cnt_1;
cnt_parity += three_shift[j];
}
int res1 = qpow(2,res + cnt_1);
if(cnt_parity % 2) ans = (ans - res1 + MOD) % MOD;
else ans = (ans + res1) % MOD;
}
std::cout << ans;
return 0;
}
所以问题来了,为什么只有
tips:在最终算答案的时候,我们最好使用欧拉定理来降幂,否则时间可能会超过
code for 100pts
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define int long long
inline int read() {
int x=0,f=1;char ch=getchar();
while(ch > '9' || ch < '0'){if(ch == '-'){f = -1;}ch = getchar();}
while(ch >= '0'&&ch <= '9'){x = x * 10 + ch - 48; ch = getchar();}
return x * f;
}
const int MOD = 998244353;
const int PRIME_NUM = 20;
int n,a[PRIME_NUM],cnt,ans,Cnt,pri[1000010];
int qpow(__int128 a,int b,int P)
{
int res = 1;
while(b)
{
if(b & 1) res = (res * a) % P;
a = (a * a) % P;
b >>= 1;
}
return res;
}
bool check(int a,int b)
{
int tmp = b - 1;
if(qpow(a,tmp,b) != 1) return 0;
while(tmp % 2 == 0)
{
tmp >>= 1;
if(qpow(a,tmp,b) == b - 1) return true;
if(qpow(a,tmp,b) != 1) return false;
}
return true;
}
bool check_prime(int x)
{
for(int i = 1;i <= 12;++i)
{
if(!check(pri[i],x)) return false;
}
return true;
}
bool vis[1000010];
void divide(int x)
{
for(int i = 2;i <= 1e6;++i)
{
if(!vis[i]) pri[++Cnt] = i;
for(int j = 1;j <= Cnt && pri[j] * i <= 1e6;++j)
{
vis[i * pri[j]] = true;
if(i % pri[j] == 0) break;
}
}
for(int i = 1;i <= Cnt;++i)
{
if(x % pri[i] == 0)
{
++cnt;
while(x % pri[i] == 0) x /= pri[i],++a[cnt];
}
}
if(x > 1)
{
int t = (int)sqrt(x);
if(t * t == x)
{
a[++cnt] = 2;
}
else
{
a[++cnt] = 1;
if(!check_prime(x)) a[++cnt] = 1;
}
}
}
signed main()
{
n = read();
divide(n);
int up_limit = qpow(3,cnt,MOD);
for(int i = 0;i < up_limit;++i)
{
int res = 1,cnt_parity = 0,cnt_1 = 0,tmp = i;//cnt_parity判断最终容斥系数奇偶
for(int j = 1;j <= cnt;++j)
{
int now = tmp % 3;
if(now == 1) ++cnt_1;
cnt_parity += now;
res = res * (a[j] + 1 - now);
tmp /= 3;
}
int res1 = qpow(2,(res + cnt_1) % (MOD - 1),MOD);//数论欧拉定理
if(cnt_parity % 2) ans = (ans - res1 + MOD) % MOD;
else ans = (ans + res1) % MOD;
}
std::cout << ans;
return 0;
}