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 }
    卷珠帘
  • 相关阅读:
    windows下wamp多域名的配置
    数据库设计
    面向接口编程
    面向对象的设计原则
    javascript设计模式——适配器模式
    javascript设计模式——状态模式
    javascript设计模式——装饰者模式
    javascript设计模式——中介者模式
    javascript设计模式——职责链模式
    javascript设计模式——享元模式
  • 原文地址:https://www.cnblogs.com/littlepear/p/5348767.html
Copyright © 2011-2022 走看看