您的位置:首页 > 其它

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.

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: