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 莫比乌斯反演的更多相关文章

  1. F. Coprime Subsequences

    题目链接: F. Coprime Subsequences time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演

    [题目]G. Coprime Arrays [题意]当含n个数字的数组的总gcd=1时认为这个数组互质.给定n和k,求所有sum(i),i=1~k,其中sum(i)为n个数字的数组,每个数字均< ...

  3. 【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) ...

  4. CF915G Coprime Arrays 莫比乌斯反演、差分、前缀和

    传送门 差分是真心人类智慧--完全不会 这么经典的式子肯定考虑莫比乌斯反演,不难得到\(b_k = \sum\limits_{i=1}^k \mu(i) \lfloor\frac{k}{i} \rfl ...

  5. Gym - 101982B Coprime Integers (莫比乌斯反演)

    题目链接:http://codeforces.com/gym/101982/attachments 题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c ...

  6. Codeforces 915G Coprime Arrays 莫比乌斯反演 (看题解)

    Coprime Arrays 啊,我感觉我更本不会莫比乌斯啊啊啊, 感觉每次都学不会, 我好菜啊. #include<bits/stdc++.h> #define LL long long ...

  7. ACdream 1148(莫比乌斯反演+分块)

    传送门:GCD SUM 题意:给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   fo ...

  8. 我也不知道什么是"莫比乌斯反演"和"杜教筛"

    我也不知道什么是"莫比乌斯反演"和"杜教筛" Part0 最近一直在搞这些东西 做了将近超过20道题目吧 也算是有感而发 写点东西记录一下自己的感受 如果您真的 ...

  9. 【CJOJ2512】gcd之和(莫比乌斯反演)

    [CJOJ2512]gcd之和(莫比乌斯反演) 题面 给定\(n,m(n,m<=10^7)\) 求 \[\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)\] 题解 首先把公因数直 ...

随机推荐

  1. CodeForces - 697F:Legen... (AC自动机+矩阵)

    Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney ...

  2. ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  3. ogg日常运维命令

    1.查看历史记录.快捷执行历史中的一条命令 GGSCI (11g) 32> h 23: view param exta24: info all25: lag exta.... GGSCI (11 ...

  4. poj 2069 Super Star——模拟退火(收敛)

    题目:http://poj.org/problem?id=2069 不是随机走,而是每次向最远的点逼近.而且也不是向该点逼近随意值,而是按那个比例:这样就总是接受,但答案还是要取min更新. 不知那个 ...

  5. Python之路:Jquery Ajax的使用

    Ajax概述 Ajax就是通过 HTTP 请求加载远程数据.通常用于定制一些http请求来灵活的完成前端与后端的数据交互需求. 注意,所有的选项都可以通过$.ajaxSetup()函数来全局设置. J ...

  6. SSAS IIS 发布

    http://www.cnblogs.com/zhangzt/p/4046259.html IIS7下配置SSAS通过HTTP远程连接 淘宝 问答 学院 博客 资源下载 高端培训            ...

  7. POJ-2229

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 19599   Accepted: 7651 Descrip ...

  8. 微信小程序运行机制

    对于扫描接口B生成的带参小程序码的问题: (1)线上版本 扫描不同带参的小程序码会重新执行小程序的整个注册程序生命周期(详细生命周期函数见官方文档), (2)扫描相同的二维码的时候,目前微信官方给出的 ...

  9. Python3 编译中文字串报错解决方案

    问题: Python3.6.5 版本中,程序有中文,运行时出现以下error: SyntaxError: Non-UTF-8 code starting with '\xb2' in file XXX ...

  10. Linux下使用sendEmail发送带附件的邮件(转载)

    转载:http://www.ttlsa.com/linux/use-sendemail-send-file/ sendEmail是一个轻量级,命令行的SMTP邮件客户端.如果你需要使用命令行发送邮件, ...