标签:
题目链接:http://acm.uestc.edu.cn/#/problem/show/1171
这道题应该从gcd出来的值入手。
#include<iostream> #include<cstring> #include<queue> #include<vector> #include<algorithm> #include<cstdio> #define MAX_N 2000006 using namespace std; typedef long long ll; int a[MAX_N]; int n; int T; const int mod=10000007; int cnt[MAX_N]; ll Pow(ll a,ll b) { ll res = 1; while (b) { if (b & 1)res = res * a % mod; b >>= 1; a = a * a % mod; } return res; } ll ways[MAX_N]; int k; int main() { scanf("%d", &T); while (T--) { int maxA = -1; memset(a, 0, sizeof(a)); memset(cnt, 0, sizeof(cnt)); memset(ways, 0, sizeof(ways)); scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { int t; scanf("%d", &t); maxA = max(maxA, t); a[t]++; } for (int i = 1; i <= maxA; i++) for (int j = 1; j * i <= maxA; j++) cnt[i] += a[i * j]; for (int i = 1; i <= maxA; i++) ways[i] = (Pow(2, cnt[i]) - 1 + mod) % mod; for (int i = maxA; i >= 1; i--) for (int j = 2; j * i <= maxA; j++) ways[i] = (ways[i] - ways[i * j] + mod) % mod; ll ans = 0; for (int i = 1; i <= maxA; i++) ans = (ans + ways[i] * Pow(i, k)) % mod; printf("%lld", ans); if(T!=0)printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/HarryGuo2012/p/4837013.html