zoukankan      html  css  js  c++  java
  • HDU

    On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Alex is very rich, and he has N pearls of each kind. Pendant can be told apart according to permutation of its pearls. Now he wants to know how many kind of pendant can he made, with length between 1 and N. Of course, to show his wealth, every kind of pendant must be made of K pearls. 
    Output the answer taken modulo 1234567891. 

    InputThe input consists of multiple test cases. The first line contains an integer T indicating the number of test cases. Each case is on one line, consisting of two integers N and K, separated by one space. 
    Technical Specification 

    1 ≤ T ≤ 10 
    1 ≤ N ≤ 1,000,000,000 
    1 ≤ K ≤ 30 
    OutputOutput the answer on one line for each test case.Sample Input

    2
    2 1
    3 2

    Sample Output

    2
    8

    题意:给定N,K。求不超过N的链子的所有染色情况,使得起使用了K种颜色。

    思路:

      法1:容斥,用K种颜色,则其方案=1^K+2^K+...N^K,符号为正。 用K-1种颜色,方案=1^(K-1)+2^(K-2)...N^(K-1),符号为负...。用1种颜色...然后累加即可,但是我不会。

      法2:不难得到其DP方程,dp[i][j]=dp[i-1][j-1]*(K-j+1)+dp[i-1][j]*j;然后用矩阵优化DP。在第一维加个1用来累加前缀和。

    #include<bits/stdc++.h>
    #define ll long long
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    const int maxn=32;
    const int Mod=1234567891;
    int L;
    struct mat
    {
        int M[maxn][maxn];
        mat() { memset(M,0,sizeof(M)); }
        mat friend operator *(mat a,mat b)
        {
            mat res;
            for(int k=0;k<=L;k++)
             for(int i=0;i<=L;i++)
              for(int j=0;j<=L;j++)
               res.M[i][j]=(res.M[i][j]+(ll)a.M[i][k]*b.M[k][j]%Mod)%Mod;
            return res;
        }
        mat friend operator ^(mat a,ll x)
        {
           mat res; rep(i,0,L) res.M[i][i]=1;
           while(x){
              if(x&1LL) res=res*a; a=a*a; x/=2;
           } return res;
        }
    };
    
    int main()
    {
        int T,N,K;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&N,&K); L=K;
            mat base,a;
            a.M[1][0]=K; //单独考虑,因为base的第0行是累加dp的,不能再用了。
            base.M[0][0]=1; base.M[0][K]=1;
            rep(i,1,K){
                if(i!=1)//这里单独考虑了。
                  base.M[i][i-1]=K-i+1;
                base.M[i][i]=i;
            }
            a=(base^(N))*a;
            printf("%d
    ",a.M[0][0]);
        }
        return 0;
    }
  • 相关阅读:
    HTML + CSS短标题(二,三,四文字长度)两端对齐的方式
    转载 ----MAC 上搭建lua
    转载 -- 基于原生JS与OC方法互相调用并传值(附HTML代码)
    转载 ---资深HR告诉你:我如何筛选简历与选择人员的
    转载 OSX开发推荐书籍列表
    IOS --支付宝SDK 分解讲解
    IOS -RSA加密及解密
    ios --转载 使用SMSSDK实现短信验证:
    ios --转载 在mac上安装让rvm及cocoa pods详解
    简单说明Python中的装饰器的用法
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9670906.html
Copyright © 2011-2022 走看看