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. nefu 899这也是裸的找

    #include <iostream> #include <algorithm> #include <cstdio> using namespace std; in ...

  2. Bitmap和Drawable浅谈

    一.概念区别 Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB8888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一 ...

  3. PMBok项目管理

    这就是项目管理的九大领域:整合管理.范围管理.时间管理.费用管理.质量管理.人力资源管理.沟通管理.风险管理.采购管理. 项目管理好像一头大象,将其大卸九块之后,要装进冰箱就容易多了. 看看书上是怎样 ...

  4. 转载,find.sh

    #!/bin/bash #find files contains a keyword #write by xiaojing.zhao #2012.12.14 echo -e "\nThis ...

  5. OPenGL中的缓冲区对象

    引自:http://blog.csdn.net/mzyang272/article/details/7655464 在许多OpenGL操作中,我们都向OpenGL发送一大块数据,例如向它传递需要处理的 ...

  6. 转载:数位DP模板

    // pos = 当前处理的位置(一般从高位到低位) 2 // pre = 上一个位的数字(更高的那一位) 3 // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, 4 ...

  7. Linux 安全模块

    LSM是Linux Secrity Module的简称,即linux安全模块.其是一种轻量级通用访问控制框架,适合于多种访问控制模型在它上面以内核可加载模块的形实现.用户可以根据自己的需求选择合适的安 ...

  8. Java Interrupt Related

    In Java, the main process can have several threads at a time, but only a few of them can run concurr ...

  9. overload与override

    一.override(重写.覆写) 1.子类重写父类的方法(两同一小一大一权限) ① 方法签名必须相同,返回值类型必须相同. ② 抛出的异常必须小于等于父类方法 ③ 权限修饰符必须大于等于父类方法的权 ...

  10. 快学Scala-第四章 映射和元组

    知识点: 1.构造映射,映射是对偶的集合 val scores1 = Map("Alice" -> 10, "Bob" -> 7, "Ci ...