您的位置:首页 > 其它

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];

#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;
}


卷珠帘
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: