zoukankan      html  css  js  c++  java
  • CA Loves GCD

    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].
    1T50
     
    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];
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<vector>
     8 using namespace std;
     9 const int maxn = 1005;
    10 const int mod = 100000007;
    11 typedef long long ll;
    12 //priority_queue<int, vector<int>, greater<int> > pq;
    13 int Gcd[maxn][maxn],dp[maxn][maxn];
    14 int gcd(int a,int b){
    15     return b == 0?a:gcd(b,a%b);
    16 }
    17 void up(int &x){
    18     if(x>=mod) x -= mod;
    19 }
    20 void pre(){
    21     for(int i = 0; i<=1000; i++)
    22         for(int j = 0; j<=1000; j++)
    23         Gcd[i][j] = gcd(i,j);
    24 }
    25 void solve(){
    26     int t,n;
    27     pre();
    28     scanf("%d",&t);
    29     while(t--){
    30         scanf("%d",&n);
    31         memset(dp,0,sizeof(dp));
    32         dp[0][0] = 1;
    33         int x;
    34         for(int i = 0; i<n; i++){
    35             scanf("%d",&x);
    36             for(int j = 0; j<=1000; j++)
    37             if(dp[i][j]){
    38                 up(dp[i+1][Gcd[j][x]] += dp[i][j]);
    39 
    40                 up(dp[i+1][j] += dp[i][j]%mod);
    41             }
    42         }
    43         int sum = 0;
    44         for(int i = 1; i<=1000; i++){
    45            //     if(dp[n][i]) printf("%d\n",dp[n+1][i]);
    46             up(sum += ((ll)i*dp[n][i])%mod);
    47         }
    48         printf("%d\n",sum%mod);
    49     }
    50 }
    51 int main()
    52 {
    53     solve();
    54     return 0;
    55 }
    卷珠帘
  • 相关阅读:
    O(big oh) (big omega) (big theta)
    Ex 7_21 在一个流网络中,一条边被称为是临界的...第十三次作业
    Ex 7_17 考虑如下的网络(其中数字为对应边的容量)...第十三次作业
    Expm 10_2 实现Ford-Fulkerson算法,求出给定图中从源点s到汇点t的最大流,并输出最小割。
    pat1009
    pat1008
    pat1007
    pat1006
    pat1005
    pat1004
  • 原文地址:https://www.cnblogs.com/littlepear/p/5348767.html
Copyright © 2011-2022 走看看