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
DP转移一下
分两种情况:
1. X被选中与j取gcd,即dp[i+1][gcd(x,j)] += dp[i][j];
2. x未被选中,即dp[i+1][j] += dp[i][j];
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int maxn = ;
const int mod = ;
typedef long long ll;
//priority_queue<int, vector<int>, greater<int> > pq;
int Gcd[maxn][maxn],dp[maxn][maxn];
int gcd(int a,int b){
return b == ?a:gcd(b,a%b);
}
void up(int &x){
if(x>=mod) x -= mod;
}
void pre(){
for(int i = ; i<=; i++)
for(int j = ; j<=; j++)
Gcd[i][j] = gcd(i,j);
}
void solve(){
int t,n;
pre();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(dp,,sizeof(dp));
dp[][] = ;
int x;
for(int i = ; i<n; i++){
scanf("%d",&x);
for(int j = ; j<=; j++)
if(dp[i][j]){
up(dp[i+][Gcd[j][x]] += dp[i][j]); up(dp[i+][j] += dp[i][j]%mod);
}
}
int sum = ;
for(int i = ; i<=; i++){
// if(dp[n][i]) printf("%d\n",dp[n+1][i]);
up(sum += ((ll)i*dp[n][i])%mod);
}
printf("%d\n",sum%mod);
}
}
int main()
{
solve();
return ;
}

卷珠帘

CA Loves GCD的更多相关文章

  1. CA Loves GCD (BC#78 1002) (hdu 5656)

    CA Loves GCD  Accepts: 135  Submissions: 586  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: ...

  2. 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 ...

  3. 数学(GCD,计数原理)HDU 5656 CA Loves GCD

    CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...

  4. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  5. 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 ...

  6. 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 ...

  7. HDU 5656 ——CA Loves GCD——————【dp】

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  8. hdu-5656 CA Loves GCD(dp+数论)

    题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Ot ...

  9. hdu 5656 CA Loves GCD

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

随机推荐

  1. flash检测网络是否通畅

    air: 要在 Adobe ® Flash ® Professional(CS4 或更高版本)中使用 air.net 包: 选择“文件”>“发布设置”命令. 在 Flash 面板中,单击 Act ...

  2. JS调用OC方法

    - (void)myMethod:(CDVInvokedUrlCommand*)command { NSString* echo = [command.arguments objectAtIndex: ...

  3. spice-vdagent

    The spice-vdagent should be running in the guest. Have you installed the spice guest tools in your w ...

  4. openCV(一)---将openCV框架导入iOS工程中

    开发环境: Xcode 6.4   openCV for iOS 3.0    配置openCV开发环境 在OpenCV官网中下载OpenCV开发包(官网地址:http://opencv.org/) ...

  5. Toolbar Painter 工具条制作

    工具条制作工具(ToolBarPainter2013.exe),专为程序猿设计,界面开发必备.当用VC编程制作工具条时,需要为工具栏上每一个button添加图标,是一件极其繁琐的事情,该工具可利用已有 ...

  6. wpf绑定之格式化日期

    只显示年月日: StringFormat='{}{0:yyyy/MM/dd}' 又或者: StringFormat='{}{0:yyyy年MM月dd日 dddd HH:mm:ss}',Converte ...

  7. Tree Cutting

    Tree Cutting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Prob ...

  8. JPA 系列教程2-单表操作

    JPA Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibe ...

  9. UICollectionView 浅析

    什么是UICollectionView UICollectionView是一种新的数据展示方式,简单来说可以把他理解成多列的UITableView(请一定注意这是UICollectionView的最最 ...

  10. oracle10g遇到ORA-16038日志无法归档问题

    SQL> shutdown immediate ORA-01109: 数据库未打开 已经卸载数据库. ORACLE 例程已经关闭. SQL> startup ORACLE 例程已经启动. ...