zoukankan      html  css  js  c++  java
  • HDU 5656 CA Loves GCD dp

    CA Loves GCD

    题目连接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5656

    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].
    1≤T≤50

    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个数,然后你可以选择若干个数出来,然后求他的gcd

    然后现在让你遍历所有的方案,问你所有方案的和是多少

    题解:

    dp[i][j]表示现在选了i个数,gcd为j的方案数

    dp[i][j]->dp[i+1][j],dp[i+1][gcd(a[i+1],j)]

    然后不停转移就好了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1050;
    const int mod = 100000007;
    int dp[maxn][maxn],a[maxn],n;
    long long ans;
    int gcd(int a,int b)
    {
        return b==0?a:gcd(b,a%b);
    }
    void solve()
    {
        memset(dp,0,sizeof(dp));dp[0][0]=1;ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<maxn;j++)if(dp[i][j])
            {
                (dp[i+1][j]+=dp[i][j])%=mod;
                (dp[i+1][gcd(j,a[i+1])]+=dp[i][j])%=mod;
            }
        for(int i=1;i<maxn;i++) (ans+=1ll*i*dp[n][i]%mod)%=mod;
        printf("%d
    ",ans);
    }
    int main()
    {
        int t;scanf("%d",&t);
        while(t--)solve();
    }
  • 相关阅读:
    如何在一个页面后面随机跳转到多个链接地址Math.floor()和Math.random()
    thinkphp中volist标签
    PHP中删除数组空值的方法
    PHP实现四种基本排序算法
    如何解决自动加载与模板中(如Smarty)的自动加载冲突的问题
    GD库常用函数
    内网最小化安装CentOS后,想安装ISO文件中的包怎么办呢?
    Elasticsearch插件安装
    python类的反射使用方法
    python类的继承
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5350960.html
Copyright © 2011-2022 走看看