ZOJ 3868 GCD Expectation

比赛的时候不会做。。。看kuangbin大神的题解才会的。。。http://www.kuangbin.net/archives/15th_zju#more-583


#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 1000005
#define maxm 100005
#define eps 1e-7
#define mod 998244353
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

LL dp[maxn];
int c[maxn];
int n, k;

void work()
{
	int x, mx = 0;
	memset(c, 0, sizeof c);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &x);
		mx = max(mx, x);
		c[x]++;
	}

	memset(dp, 0, sizeof dp);

	for(int i = mx; i >= 1; i--) {
		int cnt = 0;
		for(int j = i; j <= mx; j += i) {
			dp[i] = (dp[i] - dp[j]) % mod;
			cnt += c[j];
		}

		dp[i] = (dp[i] + powmod(2, cnt) - 1) % mod;
	}
	LL ans = 0;
	for(int i = 1; i <= mx; i++) ans = (ans + powmod(i, k) * dp[i] % mod) % mod;
	printf("%lld\n", (ans + mod) % mod);	
}

int main()
{
	int _;
	scanf("%d", &_);
	while(_--) work();
	
	
	return 0;
}


你可能感兴趣的:(ZOJ)