ZOJ-3868-GCD Expectation(容斥)
来源:互联网 发布:air打电话软件 编辑:程序博客网 时间:2024/06/04 23:18
Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1,x2,…,xm} (each nonempty subset has equal probability to be picked), and would like to know the expectation of [gcd(x1,x2,…,xm)]k.
Note that gcd(x1, x2,…,xm) is the greatest common divisor of {x1,x2,…,xm}.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers n, k (1 ≤ n,k ≤ 106). The second line contains n integers a1, a2,…,an (1 ≤ai ≤ 106).
The sum of values max{ai} for all the test cases does not exceed 2000000.
Output
For each case, if the expectation is E, output a single integer denotesE · (2n - 1) modulo 998244353.
Sample Input
15 11 2 3 4 5
Sample Output
42
题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5480
题意 ,给出序列a[1],a[2]...a[i],求子序列 [gcd(x1, x2,…,xm)]k. 的期望乘 (2n - 1) , 显然子序列个数有 (2n - 1) 个,那么其实就是所有子序列gcd的k次方求和啦
数据范围不超1e6,那么可以直接枚举gcd就好啦,dp[i]表示gcd为i的组数,从高到低枚举gcd,计算出i的倍数出现了m次,那么就有2^m-1组为i倍数的子序列,
显然,dp[i]=2^m-1-dp[i*2]-dp[i*3]-....;
//#include<bits/stdc++.h>#include <iostream>#include <cmath>#include <algorithm>#include <cstdio>#include <cstring>#define LL long longusing namespace std;const int MAXN = 1e6+7;const int MOD = 998244353;long long a[MAXN],dp[MAXN];long long quick_pow(long long a,long long n){ long long ans=1; while(n) { if(n&1)ans*=a,ans%=MOD; a=a*a%MOD; n>>=1; } return ans;}int main(){ int T,n,k,temp; cin>>T; int cas=1; while(T--) { memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); scanf("%d%d",&n,&k); int maxx=0,x; for(int i=0; i<n; ++i) { scanf("%d",&x); a[x]++; maxx=max(maxx,x); } long long ans=0; for(int i=maxx; i>=1; --i) { int cnt=0,temp=0; for(int j=i; j<=maxx; j+=i) { cnt+=a[j]; temp=(temp-dp[j]+MOD)%MOD; } dp[i]=((quick_pow(2,cnt)-1)%MOD+temp)%MOD; ans=(ans+dp[i]*quick_pow(i,k)%MOD)%MOD; } cout<<ans<<endl; } return 0;}
- ZOJ-3868-GCD Expectation(容斥)
- zoj 3868 GCD Expectation(容斥原理)
- ZOJ 3868 GCD Expectation
- ZOJ 3868 GCD Expectation
- ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)
- zoj 3868 GCD Expectation(数学)
- ZOJ 3868 GCD Expectation DP
- ZOJ 3868 - GCD Expectation(数论)
- ZOJ 3868 GCD Expectation(莫比乌斯反演)
- [暴力统计] zoj 3868 GCD Expectation
- ZOJ GCD Expectation
- ZOJ 3868 GCD Expectation 莫比乌斯反演
- ZOJ 3868 GCD Expectation 莫比乌斯反演
- ZOJ 3868 GCD Expectation 和 BC39 HDU 5212 Code
- GCD(容斥定理)
- ZOJ3868:GCD Expectation
- hdu 1695 GCD(容斥定理)
- HDU 1695 GCD(容斥+组合)
- Markdown 低频语法手册
- 解决一下华为手机选取相册照片,选取不到问题。
- 内存管理 ---slab机制 分配对象
- Android 导入第三方jar包
- 设计模式(6)--策略模式
- ZOJ-3868-GCD Expectation(容斥)
- Jquery Ajax 跨域调用asmx类型 WebService范例
- Android Gradle Plugin指南(一)——简介
- 正则表达式
- IOS开发小功能:自定义UITabBar,可以直接拿来用
- GitHub版本控制器使用
- 多波束EM122安装纪实(一)总览
- 几种常见排序算法
- 理解面向对象编程(二)-用例图及特征