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. L88

    Where You Vote May Affect How You Vote On election day, where do you vote? If it's in a church, you ...

  2. HihoCoder1642 : 三角形面积和([Offer收割]编程练习赛37)(求面积)(扫描线||暴力)(占位)

    描述 如下图所示,在X轴上方一共有N个等腰直角三角形.这些三角形的斜边与X轴重合,斜边的对顶点坐标是(Xi, Yi). (11,5) (4,4) /\ /\(7,3) \ / \/\/ \ / /\/ ...

  3. CodeForces - 123E Maze

    http://codeforces.com/problemset/problem/123/E 题目翻译:(翻译来自: http://www.cogs.pw/cogs/problem/problem.p ...

  4. 浏览器,tab页显示隐藏的事件监听--页面可见性

    //监听浏览器tab切换,以便在tab切换之后,页面隐藏的时候,把弹幕停止 document.addEventListener('webkitvisibilitychange', function() ...

  5. DCloud-HTML5+:5+ App开发入门指南

    ylbtech-DCloud-HTML5+:5+ App开发入门指南 1.返回顶部 1. 5+ App开发入门指南 App App入门 HTML5 Plus应用概述 HTML5 Plus移动App,简 ...

  6. Pointer 指针

    利用指针访问对象,指针指向一个对象,允许使用解引用符(操作符*)来访问对象 int ival = 42; int *p = &ival;//p存放变量ival的内存地址,p是指向变量ival的 ...

  7. [转]对 td 使用 overflow:hidden; 无效的几点错误认识

    转载:http://www.cftea.com/c/2010/12/UVBUCD0J888L2XPQ.asp 一.是 td 的原因. 其实这关 td 什么事呢?div 也是一样的,看示例: <d ...

  8. Tomcat访问程序外的上传文件

    自己在编写程序时,把图片上传到程序根目录下,但是页面使用<img> 没有显示.但是,当我刷新项目下文件夹后,页面刷新可以显示. 我通过网上查询,当在Tomcat下的server.xml配置 ...

  9. <c和指针>学习笔记3之函数和数组

    1 函数声明 (1)原型 告诉编译器函数的参数数量和每个参数的类型以及返回值的类型.编译器通过检查原型之后,就可以检查这个函数得调用,从而来确保参数正确,返回值无误. 通用技巧,将原型写在一个头文件当 ...

  10. WCF大文件传输【转】

    http://www.cnblogs.com/happygx/archive/2013/10/29/3393973.html WCF大文件传输 WCF传输文件的时候可以设置每次文件的传输大小,如果是小 ...