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

    CA Loves GCD

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

    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
     
    Source

    By YJQ 我们令dp[i][j]表示在前i个数中,选出若干个数使得它们的gcd为j的方案数,于是只需要枚举第i+1个数是否被选中来转移就可以了

    令第i+1个数为v,当考虑dp[i][j]的时候,我们令dp[i+1][j] += dp[i][j]dp[i+1][j]+=dp[i][j](v 不选),dp[i+1][gcd(j,v)] += dp[i][j]dp[i+1][gcd(j,v)]+=dp[i][j](v 选)

    复杂度O(N*MaxV) MaxV 为出现过的数的最大值

    ps:取模的那个数是坑点。。。。平常是1e9+7。。。

    #include<bits/stdc++.h>
    using namespace std;
    #define ll __int64
    #define mod 100000007
    #define esp 0.00000000001
    const int N=1e3+10,M=1e6+10,inf=1e9;
    ll dp[N][N];
    int a[N];
    int flag[N][N];
    int gcd(int x,int y)
    {
        return y==0?x:gcd(y,x%y);
    }
    int main()
    {
        int x,y,z,i,t;
        for(i=1;i<=1000;i++)
        for(t=1;t<=1000;t++)
        flag[i][t]=gcd(i,t);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(dp,0,sizeof(dp));
            int maxx=0;
            scanf("%d",&x);
            for(i=1;i<=x;i++)
            {
                scanf("%d",&a[i]);
                maxx=max(a[i],maxx);
            }
            for(i=1;i<=x;i++)
            {
                dp[i][a[i]]+=1;
                for(t=1;t<=maxx;t++)
                {
                    dp[i][t]+=dp[i-1][t];
                    dp[i][t]%=mod;
                    dp[i][flag[t][a[i]]]+=dp[i-1][t];
                    dp[i][flag[t][a[i]]]%=mod;
                }
            }
            ll ans=0;
            for(ll i=1;i<=maxx;i++)
            {
                ans+=(i*dp[x][i])%mod;
                ans%=mod;
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    【学习总结】iOS 的机制
    【刷题】面筋-游戏测试-对二次元游戏的认识
    【JAVA】Mac下查看已安装的jdk版本及其安装目录
    【JAVA】MacBook安装Java环境及eclipse
    【学习总结】哈希表:哈希函数构造;哈希表解决地址冲突的方法
    【刷题】面筋-JAVA-hashmap和hashtable
    【刷题】面筋-测开-软件测试的生命周期
    【学习总结】jupyter notebook中以Markdown格式显示文字信息
    【学习总结】n & (n-1)
    【学习总结】IOS系统和Android系统的区别
  • 原文地址:https://www.cnblogs.com/jhz033/p/5635823.html
Copyright © 2011-2022 走看看