HDU 5656 CA Loves GCD (数论DP)
CA Loves GCD
题目链接:
http://acm.hust.edu.cn/vjudge/contest/123316#problem/B
Description
CA is a fine comrade who loves the party and people; inevitably she loves GCD (greatest common divisor) too.
Now, there are 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 denoting the number of testcases.
testcases follow. Each testcase contains a integer in the first time, denoting , the number of the numbers CA have. The second line is numbers.
We guarantee that all numbers in the test are in the range [1,1000].
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
Hint
题意:
给出N(N<=1000)个不超过1000的数字;
对于每个子集可以求出该子集的最大公约数;
现在要求所有子集的最大公约数之和.
题解:
由于数字的规模不超过1000;
则可以直接用DP暴力枚举所有子集情况;
dp[i] 表示以i为最大公约的子集有多少个;
扫描这N个数字:
对于当前的num[i], 枚举其可能出现的最大公约数并计数:
int tmp = gcd(num[i], j);
dp[tmp] = (dp[tmp] + dp[j]) % mod;
很遗憾,直接枚举1-1000来更新dp会TLE;
优化途径:
1.将1000内任意两个数的gcd值打表.
2.每次枚举k时,若d[j]为0(即不存在以j为gcd的子集),则不需要更新(节省算gcd的时间);
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1500
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
int num[maxn];
LL dp[maxn];
int gcc[1001][1001];
int main(int argc, char const *argv[])
{
//IN;
for(int i=1; i<=1000; i++) {
for(int j=1; j<=1000; j++) {
gcc[i][j] = __gcd(i,j);
}
}
int t; scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%d", &num[i]);
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for(int i=1; i<=n; i++) {
for(int j=1; j<=1000; j++) {
int tmp = gcc[num[i]][j];
dp[tmp] = (dp[tmp] + dp[j]) % mod;
}
dp[num[i]]++;
}
LL ans = 0;
for(int i=1; i<=1000; i++) {
ans = (ans + i%mod*dp[i]) % mod;
}
printf("%I64d\n", ans);
}
return 0;
}
HDU 5656 CA Loves GCD (数论DP)的更多相关文章
- HDU 5656 ——CA Loves GCD——————【dp】
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- hdu 5656 CA Loves GCD(dp)
题目的意思就是: n个数,求n个数所有子集的最大公约数之和. 第一种方法: 枚举子集,求每一种子集的gcd之和,n=1000,复杂度O(2^n). 谁去用? 所以只能优化! 题目中有很重要的一句话! ...
- 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 dp
CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...
- 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 ...
- 数学(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
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5656 CA Loves GCD (容斥)
题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个 ...
- CA Loves GCD (BC#78 1002) (hdu 5656)
CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: ...
随机推荐
- NFC(2)NFC、蓝牙和红外之间的差异
NFC(2)NFC.蓝牙和红外之间的差异表
- css link和@import区别用法
这里link与@import介绍的是html引入css的语法单词.两者均是引入css到html的单词. 1.link语法结构<link rel="stylesheet" ty ...
- C#6.0 VS2015
https://msdn.microsoft.com/en-us/library/hh156499(v=vs.140).aspx This page lists key feature names f ...
- Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程
最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...
- inline-block在ie6中的经典bug
众所周知,给元素设置 inline-block ,可以让ie下的元素出发layout:1. 但是,当给元素设置 inline-block 后,在另外一个class 样式(非设置inline-block ...
- 简单实现WPF界面控件换肤效果
效果如下如图:选择皮肤颜色 1.首先新建一个如图界面: 选择匹夫下拉框Xaml代码如下:三种颜色选项,并触发SelectionChanged事件 <ComboBox Height="2 ...
- ubuntu下安装使用vmware、kvm、xen
一. 概念介绍: (1)全虚拟化(Full Virtulization) 简介:主要是在客户操作系统和硬件之间捕捉和处理那些对虚拟化敏感的特权指令,使客户操作系统无需修改就能运行, 速度会根据不同的实 ...
- linq xml读取
<?xml version="1.0" encoding="UTF-8" ?> <cache> <chatOld> < ...
- UVA 11396 Claw Decomposition(二分图)
以“爪”形为单元,问所给出的无向图中能否被完全分割成一个个单元. 分析图的性质,由于已知每个点的度是3,所以“爪”之间是相互交错的,即把一个“爪”分为中心点和边缘点,中心点被完全占据,而边缘点被三个“ ...
- CodePage代码,MultiByteToWideChar
Identifier .NET Name Additional information 37 IBM037 IBM EBCDIC US-Canada 437 IBM437 OEM United Sta ...