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}.
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.
For each case, if the expectation is E, output a single integer denotes E · (2n - 1) modulo 998244353.
1 5 1 1 2 3 4 5
42
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}.
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.
For each case, if the expectation is E, output a single integer denotes E · (2n - 1) modulo 998244353. ((2n - 1) 其实不用考虑了)
1 5 1 1 2 3 4 5
42
这题一不小心就要超时了。。。
#include<iostream> #include<algorithm> #include<string> #include<map> #include<vector> #include<cmath> #include<string.h> #include<stdlib.h> #include<cstdio> #include <ctime> #include <cstdlib> #define ll long long #define mod 998244353 using namespace std; int num[1000001]; //把这两个数组放到main函数里就超时了。。 int r[1000001]; ll power(ll a, ll n) { ll r = 1; for (; n; n >>= 1) { if (n & 1) r = r * a % mod; a = a * a % mod; } return r; } int main(){ int t; scanf("%d",&t); while(t--){ memset(r,0,sizeof(r)); //我把r换成map居然超时了。。 int n,m,a; scanf("%d%d",&n,&m); int max=0; for(int i=0;i<n;++i){ scanf("%d",&a); r[a]++; if(a>max) max=a; } for(int i=max;i>=1;--i){ //枚举gcd的可能值(倒序) int sum=0; num[i]=0; for(int j=i;j<=max;j+=i){ //求它的倍数 sum+=r[j]; //符合条件的元素个数 num[i]=(num[i]-num[j]+mod)%mod; //我们求的只有gcd=i而不包括它的倍数 } num[i]=(num[i]+power(2,sum)-1)%mod; //power(2,sum)-1求长度为sum的非空子集个数 } int s=0; for(int i=1;i<=max;++i){ //gcd s=(s+power(i,m)*num[i]%mod)%mod; } printf("%d\n",s); } return 0; }