F. Coprime Subsequences 莫比乌斯反演
http://codeforces.com/contest/803/problem/F
这题正面做了一发dp
dp[j]表示产生gcd = j的时候的方案总数。
然后稳稳地超时。
考虑容斥。
总答案数是2^n - 1
那么需要减去gcd = 2的,减去gcd = 3的,减去gcd = 5的。加上gcd = 6的,那么gcd = 4的呢?
不用处理,因为这些在gcd = 2的时候减去就行。就是把他们的贡献统计到gcd = 2的哪里去。
然后这个
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
LL quick_pow(LL a, LL b, LL MOD) {
LL ans = ;
LL base = a;
while (b) {
if (b & ) {
ans = ans * base % MOD;
}
b >>= ;
base = base * base % MOD;
}
return (ans - + MOD) % MOD;
}
const int maxn = + ;
int cnt[maxn];
int prime[maxn];//这个记得用int,他保存的是质数,可以不用开maxn那么大
bool check[maxn];
int total;
int mu[maxn];
void initprime() {
mu[] = ; //固定的
for (int i = ; i <= maxn - ; i++) {
if (!check[i]) { //是质数了
prime[++total] = i; //只能这样记录,因为后面要用
mu[i] = -; //质因数分解个数为奇数
}
for (int j = ; j <= total; j++) { //质数或者合数都进行的
if (i * prime[j] > maxn - ) break;
check[i * prime[j]] = ;
if (i % prime[j] == ) {
mu[prime[j] * i] = ;
break;
}
mu[prime[j] * i] = -mu[i];
//关键,使得它只被最小的质数筛去。例如i等于6的时候。
//当时的质数只有2,3,5。6和2结合筛去了12,就break了
//18留下等9的时候,9*2=18筛去
}
}
} void calc(int val) {
int en = (int)sqrt(val * 1.0);
bool flag = false;
for (int i = ; i <= en; ++i) {
if (val % i == ) {
flag = true;
cnt[i]++;
if (val / i != i)
cnt[val / i]++;
}
}
cnt[val]++;
}
const int MOD = 1e9 + ;
void work() {
int n;
cin >> n;
for (int i = ; i <= n; ++i) {
int x;
cin >> x;
calc(x);
}
LL ans = quick_pow(, n, MOD);
// cout << cnt[5] << endl;
for (int i = ; i <= maxn - ; ++i) {
ans = (ans + MOD + mu[i] * quick_pow(, cnt[i], MOD)) % MOD;
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
initprime();
work();
return ;
}
就是mobius函数。
F. Coprime Subsequences 莫比乌斯反演的更多相关文章
- F. Coprime Subsequences
题目链接: F. Coprime Subsequences time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演
[题目]G. Coprime Arrays [题意]当含n个数字的数组的总gcd=1时认为这个数组互质.给定n和k,求所有sum(i),i=1~k,其中sum(i)为n个数字的数组,每个数字均< ...
- 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演,前缀和,差分
Coprime Arrays CodeForces - 915G Let's call an array a of size n coprime iff gcd(a1, a2, ..., *a**n) ...
- CF915G Coprime Arrays 莫比乌斯反演、差分、前缀和
传送门 差分是真心人类智慧--完全不会 这么经典的式子肯定考虑莫比乌斯反演,不难得到\(b_k = \sum\limits_{i=1}^k \mu(i) \lfloor\frac{k}{i} \rfl ...
- Gym - 101982B Coprime Integers (莫比乌斯反演)
题目链接:http://codeforces.com/gym/101982/attachments 题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c ...
- Codeforces 915G Coprime Arrays 莫比乌斯反演 (看题解)
Coprime Arrays 啊,我感觉我更本不会莫比乌斯啊啊啊, 感觉每次都学不会, 我好菜啊. #include<bits/stdc++.h> #define LL long long ...
- ACdream 1148(莫比乌斯反演+分块)
传送门:GCD SUM 题意:给出N,M执行如下程序:long long ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++) fo ...
- 我也不知道什么是"莫比乌斯反演"和"杜教筛"
我也不知道什么是"莫比乌斯反演"和"杜教筛" Part0 最近一直在搞这些东西 做了将近超过20道题目吧 也算是有感而发 写点东西记录一下自己的感受 如果您真的 ...
- 【CJOJ2512】gcd之和(莫比乌斯反演)
[CJOJ2512]gcd之和(莫比乌斯反演) 题面 给定\(n,m(n,m<=10^7)\) 求 \[\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)\] 题解 首先把公因数直 ...
随机推荐
- linux网络编程 inet_aton(); inet_aton; inet_addr;
. inet_aton()是一个改进的方法来将一个字符串IP地址转换为一个32位的网络序列IP地址. . inet_ntoa() 本函数将一个用in参数所表示的Internet地址结构转换成以“.” ...
- tcp攻击
- CodeForces - 204C Little Elephant and Furik and Rubik
CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚 ...
- java基础知识 学习 关于URL中传递的参数含有特殊字符
有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码 ...
- 关于java中equals与==的区别的小实验
java中equals与==经常容易混淆,简单一点说就是equals比较的是值是否相等,是一种方法,==比较的两个对象在JVM中的地址,是一种操作符. 做了几个小实验比较结果. 实验一: String ...
- CodeForces 1118F2. Tree Cutting (Hard Version)
题目简述:给定$n \leq 3 \times 10^5$个节点的树,其中一部分节点被染色,一共有$k$种不同的颜色.求将树划分成 $k$ 个不相交的部分的方案数,使得每个部分中除了未染色的节点以外的 ...
- c# 鼠标点击控件即拖动窗体
在编程中,有时打开的窗体没有边框,但是我们仍然想在鼠标放在窗体上就能拖动窗体,这样我们只需要以窗体中的一个控件为参考,我们在这里以panel为例子: public class PanelNew : P ...
- 小程序[邮箱提取器-EmailSplider]总结
1.背景情况 学东西做快的是付诸实践,写这个小程序的目的就是为了综合运用各个知识点,从而提升学习的效果. 2.涉及知识 A.Swing 的布局 B.Swing中,线程访问U ...
- 使用c语言实现的常用函数
/* 为了面试准备的,有些在工作中也可以用用,本人算法方面比较欠缺,如果有更优秀的算法麻烦告诉我啊 */ /* strcat的实现 */ #include <assert.h> char* ...
- CodeForces - 828C String Reconstruction 并查集(next跳)
String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...