题意:有一个长度为n的数组a和一个长度为m的数组b,一个素数p。有n个集合,初始都只有一个1。现在,对(i从1到n)第i个集合执行以下操作:

对所有集合中的元素c,把c * (a[i] ^ b[j]) mod p 加入集合(j从1到m), 直到集合的元素不再增加为止。

问最后这n个集合的并有多少个元素?

n到1e4, m到1e5, p到1e9。

思路(官方题解)这题运用了很多数论的知识,不对数论有一定了解比较难做出这道题。

涉及的知识:原根,阶,欧拉定理,贝祖定理。

首先我们知道,x ^ y mod p = x ^ (y mod phi(p)) mod p(欧拉定理),而集合的操作可以看成a的指数相加,所以我们第一步先求出所有b和p - 1的gcd = t,这样每个集合的大小是(p - 1) / t,(贝祖定理),集合中的元素为a[i] ^ (k * t)。但是,由于a是不一定相同的,所以我们很难直接合并。我们可以转化成原根的形式,那么假设g ^ (r[i]) = a[i], ,令q[i] = gcd(r[i], p - 1)(贝祖定理)。我们不妨令g = g ^ t,因为t是一个常数。那么,现在集合中的元素为g ^ (q[i] * k), 我们现在需要求出q[i]。怎么求q[i]呢?我们只要求出a[i] ^ t的阶 = l,然后(p - 1) / l就是q[i], 为什么呢?因为(a[i] ^ t) ^ l mod p = 1 mod p = g ^ (p - 1) mod p => a[i] ^ t mod p= g ^ ((p - 1) / l) mod p, 这样就可以算出对应的q[i],即a[i] ^ t的原根中的指标。现在我们求出了q[i],那么所有g ^ (q[i] * k)会出现在集合中。一种想法是用筛法,但是p很大,不能筛。这里需要用到容斥原理。

注意,cf上很多代码的容斥是假的,可以被hack。

以下是来自cf评论区的两个数据:

2 1 13

3 5

1

答案:6

2 1 37

31 27

1

答案:8

正确容斥做法:先处理出p - 1的所有因子,从大到小枚举,枚举因子时判断是否有q[i],始得这个因子是q[i]的倍数,如果有,说明这个因子可以对答案产生贡献,再枚举比它大的因子,如果有因子是这个因子的倍数,那么减去这个因子的贡献。最后剩下的是这个因子的贡献。如果没有,跳过即可。均摊的复杂度应该是O(log ^ 2(p))。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 100010;
int n, m, p;
int a[maxn], b[maxn], q[maxn], dp[maxn];
int qpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1) {
if(y & 1) ans = ((LL)ans * x) % p;
x = ((LL)x * x) % p;
}
return ans;
}
vector<int> re;
void div(int x) {
for (int i = 1; i * i <= x; i++) {
if(x % i == 0) {
re.push_back(i);
if(i * i != x)
re.push_back(x / i);
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int t = p - 1;
for (int i = 1; i <= m; i++) {
scanf("%d", &b[i]);
t = __gcd(t, b[i]);
}
div(p - 1);
sort(re.begin(), re.end());
for (int i = 1; i <= n; i++) {
a[i] = qpow(a[i], t);
for (int j = 0; j < re.size(); j++) {
if(qpow(a[i], re[j]) == 1) {
q[i] = (p - 1) / re[j];
break;
}
}
}
sort(q + 1, q + 1 + n);
n = unique(q + 1, q + 1 + n) - (q + 1);
int ans = 0;
for (int j = re.size() - 1; j >= 0; j--) {
bool flag = 0;
for (int i = 1; i <= n; i++) {
if(re[j] % q[i] == 0) {
flag = 1;
for (int k = j + 1; k < re.size(); k++) {
if(re[k] % re[j] == 0)
dp[j] -= dp[k];
}
break;
}
}
if(flag) {
dp[j] += (p - 1) / re[j];
ans += dp[j];
}
}
printf("%d\n", ans);
}

Codeforces 360D Levko and Sets (数论好题)的更多相关文章

  1. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

  2. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  3. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  4. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

  5. Codeforces 571E - Geometric Progressions(数论+阿巴细节题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉此题思维难度不太大,不过大概是细节多得到了精神污染的地步所以才放到 D1E 的罢((( 首先我们对所有 \(a_i,b_i\ ...

  6. Coprime Arrays CodeForces - 915G (数论水题)

    反演一下可以得到$b_i=\sum\limits_{d=1}^i{\mu(i)(\lfloor \frac{i}{d} \rfloor})^n$ 整除分块的话会T, 可以维护一个差分, 优化到$O(n ...

  7. [BZOJ1951][SDOI2005]古代猪文(数论好题)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1951 分析: 练习数论知识的好题,涉及到费马小定理.lucas定理.求逆元

  8. Educational Codeforces Round 7 B. The Time 水题

    B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...

  9. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

随机推荐

  1. lik模糊e查询语句,索引使用效果详解

    一.like查询与索引 在oracle里的一个超级大的表中,我们的where条件的列有建索引的话,会走索引唯一扫描INDEX UNIQUE SCAN.如select * from table wher ...

  2. Strings=newString(“xyz”);创建了几个 StringObject?

    两个对象,一个是"xyx",一个是指向"xyx"的引用对象 s

  3. PLC 控制系统资源

    之前整理的PC高级语言与PLC通讯代码下载链接:三菱:http://blog.sina.com.cn/s/blog_16d7d3ecb0102x6wj.html倍福:http://bbs.elecfa ...

  4. iView的Message提示框

    全局配置message main.js Vue.prototype.$Message.config({ top: 70, duration:3 }); Vue.prototype.$Message.c ...

  5. $NOIP2018$ 爆踩全场记

    NOIP2018 Day-1 路还很长. 这里就是起点. 这是最简单的一步,但这是最关键的一步. 联赛就在眼前了,一切好像都已经准备好了,一切好像又都没准备好. 相信自己吧,\(mona\),这绝对不 ...

  6. 快速失败(fail—fast)和 安全失败(fail—safe)

    快速失败(fail-fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的结构进行了修改(增加.删除),则会抛出Concurrent Modification Exception. 原理 ...

  7. after()和append()的区别、before()和prepend()区别、appendTo()和prependTo()、insertAfter()和insertBefore()

    一.after()和before()方法的区别 after()——其方法是将方法里面的参数添加到jquery对象后面去:    如:A.after(B)的意思是将B放到A后面去:    before( ...

  8. 【leetcode】623. Add One Row to Tree

    题目如下: Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with ...

  9. Centos7.4 修改selinux错误导致服务器起不来

    [root@node10 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # ...

  10. npm和gem

    https://blog.csdn.net/u011099640/article/details/53083845