博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu-5656 CA Loves GCD(dp+数论)
阅读量:4692 次
发布时间:2019-06-09

本文共 2405 字,大约阅读时间需要 8 分钟。

题目链接:

Time Limit: 6000/3000 MS (Java/Others)   

 Memory Limit: 262144/262144 K (Java/Others)

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].
1T50
 

 

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
 
题意:
 
给n个数,问n个数的全组合的gcd的和是多少;
 
思路:
 
注意到数都在1000以内,所以把所以得组合都找出来,但是数量巨大所以相同的要压缩;
 
AC代码:
#include 
#include
#include
#include
using namespace std;typedef long long ll;ll dp[1003],num[1003],p[1003][1003];int n,a[1003];const ll mod=1e8+7;//注意是8,比赛的时候这个地方直接wa到cryint gcd(int x,int y){ if(y==0)return x; return gcd(y,x%y);}int main(){ for(int i=1;i<=1000;i++) { for(int j=i;j<=1000;j++) { p[i][j]=gcd(i,j); } } int t; scanf("%d",&t); while(t--) { for(int i=1;i<=1000;i++) { num[i]=0; } ll ans=0; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1); for(int i=1;i<=n;i++) { dp[i]=(ll)a[i]; for(int j=1;j<=a[i];j++) { if(num[j]) { num[j]%=mod; dp[i]+=num[j]*p[j][a[i]];//压缩的地方 dp[i]%=mod; num[p[j][a[i]]]+=num[j]; num[p[j][a[i]]]%=mod; } } num[a[i]]++; ans+=dp[i]; ans%=mod; } cout<
<<"\n"; } return 0;}

 

 

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5348422.html

你可能感兴趣的文章
第三次作业
查看>>
使用HTML5构建iOS原生APP(2)
查看>>
sqlplus terminators - Semicolumn (;), slash (/) and a blank line
查看>>
省选知识清单/计划列表(咕?)
查看>>
远程桌面(3389)复制(拖动)文件
查看>>
转 lucene3搜索引擎,索引建立搜索排序分页高亮显示, IKAnalyzer分词
查看>>
bootstrap datetimepicker 位置错误
查看>>
9结构型模式之代理模式
查看>>
第二节 整型数据
查看>>
Python 序列
查看>>
Liferay的架构:缓存(第一部分)
查看>>
初识B/S结构编程技术
查看>>
方法、hadoop源码之JobQueueTaskScheduler-by小雨
查看>>
页面重构总结
查看>>
IO 函数
查看>>
Unity V3 初步使用 —— 为我的.NET项目从简单三层架构转到IOC做准备
查看>>
JSP页面间传递参数
查看>>
VSNETcodePrint 2005 & SQL ServerPrint 2005
查看>>
java数组基本操作
查看>>
String的indexOf()用于获取字符串中某个子字符串的位置
查看>>