CA Loves GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 707    Accepted Submission(s): 245

By YJQ我们令dp[i][j]表示在前i个数中,选出若干个数使得它们的gcd为j的方案数,于是只需要枚举第i+1个数是否被选中来转移就可以了

令第i+1个数为v,当考虑dp[i][j]的时候,我们令$dp[i+1][j] += dp[i]j,dp[i+1][gcd(j,v)] += dp[i]j

复杂度O(N*MaxV) MaxV 为出现过的数的最大值

其实有O(MaxV *log(MaxV))的做法,我们考虑记f[i]表示从这些数中选择若干个数,使得他们的gcd是i的倍数的方案数。假如有K个数是i的倍数,则f[i]=2^K-1,再用g[i]表示从这些数中选择若干个数,使得他们的gcd是i的方案数,则g[i]=f[i] - g[j] (对于所有j是i的倍数)。

由调和级数可以得到 复杂度为O(MaxV *log(MaxV));

Problem Description

CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too.
Now, there are N different numbers. Each time, CA will select several numbers (at least one), and find the GCD of these numbers. In order to have fun, CA will try every selection. After that, she wants to know the sum of all GCDs.
If and only if there is a number exists in a selection, but does not exist in another one, we think these two selections are different from each other.
Input
First line contains T denoting the number of testcases.
T testcases follow. Each testcase contains a integer in the first time, denoting N , the number of the numbers CA have. The second line is N numbers.
We guarantee that all numbers in the test are in the range [1,1000].
1≤T≤50
Output
T lines, each line prints the sum of GCDs mod 100000007 .
Sample Input
  
2 2 2 4 3 1 2 3
Sample Output
  
8 10
Source
BestCoder Round #78 (div.2)
Recommend
wange2014   |   We have carefully selected several similar problems for you:   5659  5658  5657  5654  5653

复杂度O(N*MaxV) 的做的法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
LL dp[1010][1010];//dp[i][j]表示在前i个数//中,选出若干个数使得它们的gcd为j的方案数
int A[1010];
int gcd[1010][1010];
const LL MOD=1e8+7;
int __gcd(int x,int y)
{return (x!=0)?__gcd(y%x,x):y;
}
int main()
{int n,t;cin>>t;for(int i=1;i<=1000;i++){for(int j=1;j<=1000;j++){gcd[i][j]=__gcd(i,j);}}while(t--){cin>>n;memset(dp,0,sizeof(dp));for(int i=0;i<n;i++){scanf("%d",&A[i]);}sort(A,A+n);//可以确保当前数为所遍历的最大值;for(int i=0;i<n;i++){for(int j=1;j<=A[i];j++){(dp[i+1][j]+=dp[i][j])%=MOD;(dp[i+1][gcd[j][A[i]]]+=dp[i][j])%=MOD;}dp[i+1][A[i]]++;}LL ans=0;for(int i=1;i<=1000;i++)(ans+=dp[n][i]*i)%=MOD;printf("%I64d\n",ans);}return 0;
}

O(MaxV *log(MaxV))的做法:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
LL dp[1005];
LL cnt[1005];
const LL MOD=1e8+7;
LL power(LL x,LL n)
{LL ans=1;while(n){if(n&1){(ans*=x)%=MOD;}n>>=1;(x*=x)%=MOD;}return ans;
}
int main()
{int x;int t,n;int maxn;scanf("%d",&t);while(t--){memset(dp,0,sizeof(dp));memset(cnt,0,sizeof(cnt));maxn=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&x);maxn=max(x,maxn);cnt[x]++;}LL ans=0,c;for(int i=maxn;i>=1;i--){c=0;for(int j=i;j<=maxn;j+=i){c+=cnt[j];(dp[i]=dp[i]-dp[j]+MOD)%=MOD;}(dp[i]+=(power(2,c)-1)%MOD)%=MOD;(ans+=i*dp[i])%=MOD;}printf("%I64d\n",ans);}return 0;
}

BestCoder Round #78 (div.2)_B_ CA Loves GCD相关推荐

  1. BestCoder Round #78 (div.2)

    因为rating不够QAQ就报了Div2.. [CA Loves Stick] CA喜欢玩木棍. 有一天他获得了四根木棍,他想知道用这些木棍能不能拼成一个四边形. Sample Input 2 1 1 ...

  2. HDU 5597 GTW likes function(规律+欧拉函数模板题)——BestCoder Round #66(div.1 div.2)

    GTW likes function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  3. BC div2补题以及 复习模除 逆元__BestCoder Round #78 (div.2)

    第一题没话说 智商欠费 加老柴辅导终于过了 需要在意的是数据范围为2的63次方-1 三个数相加肯定爆了 四边形的定义 任意边小于其余三边之和 换句话说就是 最长边小于其余三边之和 这样的话问题转化为 ...

  4. BestCoder Round #86 1002 HDU 5805 ——NanoApe Loves Sequence

    题意 给定一个数列,随机从该数列里删除一个数,求该数列的的相邻之间的绝对值的最大值的和. 思路 两个数列来分别维护i位前面的相邻的绝对值的最大以及i后面的最大,枚举每一个可能删除的数,然后分别从左右和 ...

  5. Codeforces Round #FF (Div. 2) A.DYZ Loves Hash

    水题.题目大意为输入一个p,n,再输入n组数据,每组数据对p取余,当和前面相同时,发现冲突,记下第几组,找到最后一组没有找到和前面一样的输出-1. *可以采用一个标记数组,就变得很简单了. A. DZ ...

  6. Codeforces Round #FF (Div. 2) B.DYZ Loves Strings

    *思路为对26个字母的价值进行排序,找到最大的价值,加最大的价值的字母往后放,价值最大. B. DZY Loves Strings time limit per test 1 second memor ...

  7. Codeforces Round #FF (Div. 2)C.DYZ Loves Sequence

    一道类似求严格递增子序列的题目.这道题可以改变一个数成任意数,使得严格递增子序列增加. *思路:两个标记数组from[i],to[i];from[i]的含义是从i出发的最长的严格递增序列长度,to[i ...

  8. (BestCoder Round #59 (div.1) B)简单DP

    (改编版,题意相同) Description 火神为了检验zone的力量,他决定单挑n个人. 由于火神训练时间有限,最多只有t分钟,所以他可以选择一部分人来单挑,由于有丽子的帮助,他得到了每个人特定的 ...

  9. BestCoder Round #68 (div.2) 1002 tree

    题意:给你一个图,每条边权值0或1,问每个点周围最近的点有多少个? 思路:并查集找权值为0的点构成的连通块. 1 #include<stdio.h> 2 #include<strin ...

最新文章

  1. 洛谷 T61816 代数式的最值
  2. Json.net|NH|Log4net|Test等工具下载地址
  3. window wamp中配置安装xhprof步骤(windows)
  4. jzoj3518-进化序列(evolve)【位运算】
  5. 插入1000万条数据到mysql数据库表
  6. 君康人寿2019年排名_君康人寿易主后 内部提出五年上市计划
  7. 午夜时分的51CTO博客与白天有什么不同?
  8. 阿里ai布局开始_如何从AI开始?
  9. 【C语言】Linux 文件读写
  10. 基于微信小程序云开(统计学生信息并导出excel)2.0版
  11. 秘鲁地震与Stallman失踪之谜
  12. 【佛系养身】男程序员护肤入门
  13. WinForm中绘制网格线,Load和Piant事件区别
  14. MATLAB柱状图画法(详细)
  15. Android网络编程之Http请求服务器数据(GET方式)
  16. js 系统教程-01-JavaScript 的历史,JavaScript 与 Java 的关系,JavaScript 与 ECMAScript 的关系,JavaScript的版本
  17. 利用matlab绘制二维均匀流线和向量场
  18. visio中公式太小_冲压模具冲床吨位计算公式丨实例分析,设计师值得一看!
  19. 宜春高考2021年成绩查询,2021年宜春中考成绩公布查询时间 宜春中考成绩查询方式入口...
  20. 推荐一款神器:在浏览器中运行 VS Code,随时随地写代码

热门文章

  1. 灰色产业链带来的合法正当的行业收入
  2. mysql a左外连接b b左外连接c_数据库中的 内连接,外连接(左连接,左外连接,右连接,右外连接),全连接,交叉连接...
  3. 计算机专业 拒绝清华,我国最任性的学霸,因离家太远拒绝清华,因专业不合适拒绝哈佛...
  4. 人机同行:明略数据产品理念之二 | 简单好用
  5. 计算数组的和以及平均值
  6. 博文推荐|通过 Apache Pulsar + ScyllaDB 构建实时聊天消息流
  7. 【PHP入门篇】 WAMPServer集成环境安装与使用--慕课网【学习总结】
  8. 夜神模拟器apk超过2g安装失败,mumu模拟器可以安装
  9. adb 无线调试,vivo 安卓uniapp无线调试开发者模式设备offline
  10. 持续集成/持续部署(3)Jenkins(2)