HDU 5656
CA Loves GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 809 Accepted Submission(s): 283
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.
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
lines, each line prints the sum of GCDs mod 100000007
.
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll __int64
#define mod 100000007
using namespace std;
ll dp[][];
int a[];
int maxn;
int t,n;
ll ans=;
ll gcd(ll aa,ll bb)// 求解gcd
{
ll exm;
if(aa<bb)
{
exm=aa;
aa=bb;
bb=exm;
}
if(bb==)
return aa;
gcd(bb,aa%bb);
}
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=; i<=t; i++)
{
ans=;
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
scanf("%d",&n);
maxn=-;
for(int j=; j<=n; j++)
{
scanf("%d",&a[j]);
if(a[j]>maxn)
maxn=a[j];
dp[j][a[j]]=;//初始化 只取第j个a[j]
}
for(int j=; j<=n; j++)
{
int v=a[j+];
for(int k=; k<=maxn; k++)
{
dp[j+][k]=(dp[j+][k]+dp[j][k])%mod;
if(dp[j][k])
{int gg=gcd(k,v);
dp[j+][gg]=(dp[j+][gg]+dp[j][k])%mod;}
}
}
for(int j=; j<=maxn; j++)
ans=(ans+j*dp[n][j])%mod;
printf("%I64d\n",ans);
}
}
return ;
}
HDU 5656的更多相关文章
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
CA Loves GCD Accepts: 64 Submissions: 535 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2 ...
- HDU 5656 CA Loves GCD 01背包+gcd
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5656 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5656 CA Loves GCD dp
CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...
- CA Loves GCD (BC#78 1002) (hdu 5656)
CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: ...
- HDU 5656 CA Loves GCD (数论DP)
CA Loves GCD 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/B Description CA is a fine c ...
- 数学(GCD,计数原理)HDU 5656 CA Loves GCD
CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...
- hdu 5656 CA Loves GCD(dp)
题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...
- HDU 5656 CA Loves GCD (容斥)
题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个 ...
- HDU 5656 ——CA Loves GCD——————【dp】
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
随机推荐
- 了解Python控制流语句——continue 语句
continue 语句用以告诉 Python 跳过当前循环块中的剩余语句,并继续该循环的下一次迭代. 案例(保存为 continue.py): while True: s = input('Enter ...
- leetcode-最长上升子序列LIS
转载原文地址:http://www.cnblogs.com/GodA/p/5180560.html 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7, ...
- Linux命令详解----ln
ln命令 ln命令为文件或文件夹创建连接,连接类型有硬链接和符号连接两种,符号连接需要使用"-s"选项 ln语法 ln [选项] 参数 使用 ln --help查看可用选项 [ro ...
- 【转】jQuery最佳实践
上周,我整理了<jQuery设计思想>. 那篇文章是一篇入门教程,从设计思想的角度,讲解"怎么使用jQuery".今天的文章则是更进一步,讲解"如何用好jQu ...
- 最全NB-IoT/eMTC物联网解决方案名录汇总
NB-IoT/eMTC等蜂窝物联网技术的成熟和商用,占据低功耗广域网络(LPWAN)的主流地位,推动全球物联网新一轮发展热潮,越来越多的行业开始采用物联网方案来解决解决实际问题.实现落地应用,越来越多 ...
- iconFont 阿里巴巴矢量图标使用方法
挑选图标的过程(共6步) 进入网站:Iconfont网址:http://www.iconfont.cn 点击网站上方的“官方图标库”,选择自己喜欢的图标.在这里我选择天猫的图标库. 选择好自己喜欢的图 ...
- c# throw抛出上一个异常
catch(exception e) { throw; } 不仅抛出这次的异常,也抛出之前的异常. 用法示例:函数A调用函数B,A用到此throw时,B中发生的异常也会继承过来. catch(exce ...
- ssl证书验证的问题
对于https请求,是需要ssl证书验证的请求的,所以如果在请求时如果不带ssl证书,那么可以忽略证书的验证 有三种方法去实现: 1.Requests请求: 在文档中可以看到:http://docs. ...
- Unity3d学习日记(五)
之前用3dsmax将模型转成FBX怎么也没有办法自动导入材质到Unity3d中(试过勾选了导出嵌入媒体,没用).索性试了试c4d,发现是可行的,看来像我这种菜鸡还是更加适合用c4d. 拿zoe ...
- 解决连接mysql报错1130
最近在服务器上部署好的应用突然间连接不上mysql数据库,报错“ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this M ...