[ABC304F] Shift Table(莫比乌斯反演)

发布时间:2024年01月19日

题目:

https://www.luogu.com.cn/problem/AT_abc304_f

思路:

容斥原理,莫比乌斯反演应该都可以,我用的是莫比乌斯反演。

注意:

最好用long long类型;

代码:

?#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
const int N = 1e5 + 100;
#define LL long long
LL f[N];
const LL mod = 998244353;
int vis[N], cn, pre[N], su[N], mu[N], n;
LL ans;
string s;
void into()
{
?? ?mu[1] = su[1] = su[0] = 1;
?? ?for (int i = 2; i <= n; i++)
?? ?{
?? ??? ?if (!su[i]) pre[++cn] = i, mu[i] = -1;
?? ??? ?for (int j = 1; j <= cn && (LL)pre[j] * i <= n; j++)
?? ??? ?{
?? ??? ??? ?su[pre[j] * i] = 1;
?? ??? ??? ?if (i % pre[j] == 0) break;
?? ??? ??? ?mu[pre[j] * i] = -mu[i];
?? ??? ?}
?? ?}
}
LL quick(LL a, LL b)
{
?? ?LL ans = 1;
?? ?while (b)
?? ?{
?? ??? ?if (b & 1) ans = ans * a % mod;
?? ??? ?b = b >> 1;
?? ??? ?a = a * a % mod;
?? ?}
?? ?return ans;
}
LL ff(int x)
{
?? ?int flag = 0;
?? ?LL cnt = 0;
?? ?for (int i = 1; i <= x; i++)
?? ?{
?? ??? ?flag = 0;
?? ??? ?for (int j = i; j <= n; j += x)
?? ??? ?{
?? ??? ??? ?if (!vis[j])
?? ??? ??? ?{
?? ??? ??? ??? ?flag = 1;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if (!flag)cnt++;
?? ?}
?? ?return (quick(2, cnt % (mod - 1)) % mod + mod) % mod;
}
LL F(int x)
{
?? ?LL ans = 0;
?? ?for (int i = 1; (LL)i * i <= x; i++)
?? ?{
?? ??? ?if (x % i) continue;
?? ??? ?int j = x / i;
?? ??? ?if (mu[i])
?? ??? ?{
?? ??? ??? ?if (f[j] == -mod) f[j] = ff(j);
?? ??? ??? ?ans = (ans + (LL)mu[i] * f[j]) % mod;
?? ??? ?}
?? ??? ?if ((LL)i * i != x && mu[j])
?? ??? ?{
?? ??? ??? ?if (f[i] == -mod) f[i] = ff(i);
?? ??? ??? ?ans = (ans + (LL)mu[j] * f[i]) % mod;
?? ??? ?}
?? ?}
?? ?ans = (ans % mod + mod) % mod;
?? ?return ans;
}
int main() {
?? ?cin >> n;
?? ?cin >> s;
?? ?s = " " + s;
?? ?for (int i = 1; i <= (int)s.size(); i++)
?? ??? ?if (s[i] == '#') vis[i] = 1;
?? ?into();
?? ?for (int i = 1; i <= n; i++)
?? ??? ?f[i] = -mod;
?? ?for (int i = 1; (LL)i * i <= n; i++)
?? ?{
?? ??? ?if (n % i) continue;
?? ??? ?ans = (ans + F(i)) % mod;
?? ??? ?int j = n / i;
?? ??? ?if ((LL)i * i != n && j != n) ans = (ans + F(j)) % mod;
?? ?}
?? ?cout << (ans % mod + mod) % mod << endl;
?? ?return 0;
}

文章来源:https://blog.csdn.net/yusen_123/article/details/135687198
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。