CA Loves GCD
2016-04-03 00:59
357 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
T lines, each line prints the sum of GCDs mod 100000007.
[align=left]Sample Input[/align]
2
2
2 4
3
1 2 3
[align=left]Sample Output[/align]
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];
卷珠帘
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
T lines, each line prints the sum of GCDs mod 100000007.
[align=left]Sample Input[/align]
2
2
2 4
3
1 2 3
[align=left]Sample Output[/align]
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 = 1005; const int mod = 100000007; 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 == 0?a:gcd(b,a%b); } void up(int &x){ if(x>=mod) x -= mod; } void pre(){ for(int i = 0; i<=1000; i++) for(int j = 0; j<=1000; j++) Gcd[i][j] = gcd(i,j); } void solve(){ int t,n; pre(); scanf("%d",&t); while(t--){ scanf("%d",&n); memset(dp,0,sizeof(dp)); dp[0][0] = 1; int x; for(int i = 0; i<n; i++){ scanf("%d",&x); for(int j = 0; j<=1000; j++) if(dp[i][j]){ up(dp[i+1][Gcd[j][x]] += dp[i][j]); up(dp[i+1][j] += dp[i][j]%mod); } } int sum = 0; for(int i = 1; i<=1000; i++){ // if(dp [i]) printf("%d\n",dp[n+1][i]); up(sum += ((ll)i*dp [i])%mod); } printf("%d\n",sum%mod); } } int main() { solve(); return 0; }
卷珠帘
相关文章推荐
- spring @Qualifier注解使用
- ASP.NET MVC HtmlHelper之Html.ActionLink
- 安卓调用百度地图定位自己的位置,然后分享自己的位置信息生成链接短串。
- 百度面试经历
- python3 发送邮件功能
- Dubbo学习过程、使用经验分享及实现原理简单介绍
- 大端vs小端
- lintcode:恢复IP地址
- MyBatis初探二
- 刷题(soj、voj)总结
- Quick cocos2dx-Lua中计时器(scheduler)
- 数组与字符串简单剖析
- C/C++中的指针
- skynet
- hadoop的unknownhostexception
- JNI log来代替printf打印调试
- 分享一个网址
- C++ 11的新特性(初识)
- System Permissions—— android系统权限
- 简单的 Winsocket UDP 通信