zoukankan      html  css  js  c++  java
  • Pendant

    Pendant

    Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 104 Accepted Submission(s): 66
     
    Problem Description
    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.
     
    Input
    The 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
     
    Output

                Output the answer on one line for each test case.
     
    Sample Input
    2
    2 1
    3 2
     
    Sample Output
    2
    8
     
     
    Source
    The 4th Baidu Cup final
     
    Recommend
    lcy
     
    /*
    题意:有k种珍珠,每种n个,现在问你能组成多少长度为1~n的珍珠项链,要求每条项链必须有k中珍珠
    
    初步思路:......没思路算是思路么?
    
    #补充:看了一下题解,dp搞,dp[i][j]表示长度为i,用j种珍珠的方案,状态转移方程为:
        dp[i][j]=dp[i-1][j]*j+dp[i-1][j-1]*(k-j+1); 长度为i 用j种珍珠的方案,用长度为i-1 用j种
        珍珠的项链再从j种珍珠中再拿一个组成长度为i,用j种珍珠的项链,在加上长度为i,用j-1种珍珠
        的项链再选另外的k-(j-1)种珍珠组成长度为i,用j种珍珠的项链。
            答案为,dp[1][k]+dp[2][k]+...+dp[n][k]
            
        写的时候是不容易实现的,因为dp数组开不到3e10,所以只能用矩阵来求递推公式。
    */
    #include<bits/stdc++.h>
    #define mod 1234567891
    #define ll long long
    using namespace std;
    /********************************矩阵模板**********************************/
    class Matrix {
        public:
            ll a[31][31];
            int n;  
    
            void init(int x) {
                memset(a,0,sizeof(a));
                if (x)  
                    for (int i = 0; i < 31 ; i++)
                        a[i][i] = 1;
            }
    
            Matrix operator +(Matrix b) {
                Matrix c;
                c.n = n;
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
                return c;
            }
    
            Matrix operator +(int x) {
                Matrix c = *this;
                for (int i = 0; i < n; i++)
                    c.a[i][i] += x;
                return c;
            }
    
            Matrix operator *(Matrix b)
            {
                Matrix p;
                p.n = b.n;   
                p.init(0);
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
                return p;
            }
    
            Matrix power(ll t) {
                Matrix ans,p = *this;
                ans.n = p.n;  
                ans.init(1);
                while (t) {
                    if (t & 1)
                        ans=ans*p;
                    p = p*p;
                    t >>= 1;
                }
                return ans;
            }
    };
    Matrix unit;
    int t;
    ll n;
    int k;
    /********************************矩阵模板**********************************/
    int main(){
        // freopen("in.txt","r",stdin);
        scanf("%d",&t);
        while(t--){
            scanf("%d%lld",&n,&k);
            k++;
            unit.init(0);
            unit.n=k;
            for(int i=0;i<k-1;i++){
                unit.a[i][i]=i+1;
                unit.a[i][i+1]=k-i-2;
            }
            unit.a[k-1][k-1]=unit.a[k-2][k-1]=1;
            unit=unit.power(n);
            // for(int i=0;i<k;i++){
                // for(int j=0;j<k;j++){
                    // cout<<unit.a[i][j]<<" ";
                // }
                // cout<<endl;
            // }
            printf("%lld
    ",(k-1)*unit.a[0][k-1]%mod);
        }
        return 0;
    }
  • 相关阅读:
    AngularJS 最常用的八种功能
    angularJs自定义模块
    angularJs 购物车模型
    angularJs-未加载完成的页面显示混乱ng-bind
    angularJs-脏检查
    angularJs-控制器
    【转载更新】Linux工具之SED 2.应用实例
    【转载更新】Linux工具之SED 1.基础知识
    【转载更新】Linux工具之AWK 4.实例
    【转载更新】Linux工具之AWK 3.高级知识
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6384144.html
Copyright © 2011-2022 走看看