cf#305 Mike and Foam(容斥)
2015-05-31 02:21
330 查看
C. Mike and Foam
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered from 1 to n. i-th kind of beer has ai milliliters of foam on it.
View Code
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered from 1 to n. i-th kind of beer has ai milliliters of foam on it.
#include<vector> #include<stdio.h> #include<string.h> #include<algorithm> typedef long long ll ; const int M = 5e5 + 10 ; int n , q ; int a[M] ; int dp[M] , cnt[M] ; bool vis[M] ; std::vector <int> g[M] ; void table () { dp[1] = 1 ; for (int i = 1 ; i < M ; i ++) { if (dp[i]) g[i].push_back(i) ; for (int j = i + i ; j < M ; j += i ) { if (dp[i]) g[j].push_back(i) ; dp[j] -= dp[i] ; } } } int main () { //freopen ("a.txt" , "r" , stdin ) ; table () ; printf ("27: %d\n" , dp[54]) ; scanf ("%d%d" , &n , &q) ; for (int i = 1 ; i <= n ; i ++) scanf ("%d" , &a[i]) ; ll ans = 0 ; while (q --) { int x ; scanf ("%d" , &x) ; if (vis[x]) { for (int i : g[ a[x] ]) ans -= 1ll * dp[i] * (-- cnt [i]) ; } else { for (int i : g[ a[x] ]) { ans += 1ll * dp[i] * (cnt[i] ++) ; } } vis[x] ^= 1 ; printf ("%I64d\n" , ans) ; } return 0 ; }
View Code
相关文章推荐
- java动态代理(JDK和cglib)
- m个工厂分的n个资源,使获得利润最大(动态规划)
- Java_计算机基础知识
- 输出如下所示矩阵
- 最长单调递增子序列问题
- 数塔问题
- 嵌入式软件开发培训笔记——linux标准I/O库
- css透明度的设置 opacity
- 二分搜索算法
- 金块问题
- OpenGL导入的obj模型透明问题
- Linux之内核中的文件系统
- LeetCode 23: Merge K Sorted Lists
- 返璞归真的Linux BFS调度器
- iOS 开发:用 Instruments 来检验你的app
- 如何在运行时加载C++函数和类
- c指针之内存释放
- Delaunay三角剖分算法
- linux screen 命令详解
- Oracle sys 用户无密码文件无法登录