题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少。

析:枚举gcd,然后求每个gcd产生的个数,这里要使用容斥定理,f[i]表示的是 gcd 是 i 的个数,g[i] 表示的是 gcd 是 i 倍数的,f[i] = g[i] - f[j] (i|j)。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int maxm = 2e4 + 10;
const LL mod = 100000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int a[maxn], len[maxn];
int f[maxn]; int main(){
f[0] = 1;
for(int i = 1; i < maxn; ++i) f[i] = (f[i-1]<<1) % mod;
int T; cin >> T;
while(T--){
scanf("%d", &n);
int mmax = 1;
ms(a, 0); ms(len, 0);
for(int i = 0; i < n; ++i){
int x; scanf("%d", &x);
++a[x]; mmax = max(mmax, x);
}
for(int i = 1; i <= mmax; ++i)
for(int j = i; j <= mmax; j += i)
len[i] += a[j];
LL ans = 0;
for(int i = mmax; i; --i){
a[i] = f[len[i]] - 1;
for(int j = i + i; j <= mmax; j += i)
a[i] -= a[j];
ans = (ans + (LL)a[i] * i) % mod;
}
printf("%lld\n", (ans+mod)%mod);
}
return 0;
}

  

最新文章

  1. Linux 计划任务 访问网页
  2. Perl Sort函数用法总结和使用实例
  3. Android textView点击滚动(跑马灯)效果
  4. 剑指offer系列36----二叉搜索树的第k个节点
  5. T4 assembly
  6. 大数求模 sicily 1020
  7. 关于SWT中的Label类和Text类
  8. hdu 4704 同余定理+普通快速幂
  9. Spring3.0官网文档学习笔记(二)
  10. 给新手--安装tomcat后username和password设置以及项目怎么部署在tomcatserver上
  11. System V IPC 之共享内存
  12. 数据库4m10d作业
  13. k8s(一) kubeadm简单集群初始化
  14. Maven的作用、用途、内涵、愿景
  15. Adobe漏洞攻击
  16. javaEE-EJB学习笔记
  17. UNIX网络编程 卷2 源代码使用
  18. iOS中 Proxy和的delegate区别
  19. Jenkins+Ant+Jmeter接口自动化集成测试实例
  20. Oracle开发者守则

热门文章

  1. Android studio实现简单的CRUD
  2. Java_8排序(冒泡排序和选择排序)
  3. Java_4.1 猜数字游戏
  4. git--(3)分支 合并
  5. javascript 的原型与原型链的理解
  6. 点评10款Github上最火爆的国产开源项目
  7. MSDos
  8. BZOJ1123或洛谷3469 [POI2008]BLO-Blockade
  9. Codeforces559C Gerald and Giant Chess
  10. AFNetworking HTTP响应头返回数据