• hdu 4602 Partition(矩阵快速幂乘法)


    Problem Description
    Define f(n) as the number of ways to perform n in format of the sum of some positive integers. For instance, when n=4, we have
      4=1+1+1+1
      4=1+1+2
      4=1+2+1
      4=2+1+1
      4=1+3
      4=2+2
      4=3+1
      4=4
    totally 8 ways. Actually, we will have f(n)=2(n-1) after observations.
    Given a pair of integers n and k, your task is to figure out how many times that the integer k occurs in such 2(n-1) ways. In the example above, number 1 occurs for 12 times, while number 4 only occurs once.
    Input
    The first line contains a single integer T(1≤T≤10000), indicating the number of test cases.
    Each test case contains two integers n and k(1≤n,k≤109).
     
    Output
    Output the required answer modulo 109+7 for each test case, one per line.
    Sample Input
    2
    4 2
    5 5
    Sample Output
    5 
    1
     
    Source

    题目大意:将一个数 n 拆分,问所有的拆分组合中 K 出现了几次。

    思路:

    列出了 n=5 时 5,4,3,2,1 出现的次数为 1 2 5 12 28
    f[n+1]=3*f[n]-f[n-1]-f[n-2]-..f[1]
    f[n]=3*f[n-1]-f[n-2]-..f[1]
    ==> f[n+1]=4*f[n]-4*f[n-1]

     

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 1000000
    23 #define inf 1e12
    24 ll n,k;
    25 struct Matrix{
    26    ll mp[3][3];
    27 };
    28 Matrix Mul(Matrix a,Matrix b){
    29    Matrix res;
    30    for(ll i=0;i<2;i++){
    31       for(ll j=0;j<2;j++){
    32          res.mp[i][j]=0;
    33          for(ll k=0;k<2;k++){
    34             res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%MOD+MOD)%MOD;
    35          }
    36       }
    37    }
    38    return res;
    39 }
    40 Matrix fastm(Matrix a,ll b){
    41    Matrix res;
    42    memset(res.mp,0,sizeof(res.mp));
    43    for(ll i=0;i<2;i++){
    44       res.mp[i][i]=1;
    45    }
    46    while(b){
    47       if(b&1){
    48          res=Mul(res,a);
    49       }
    50       a=Mul(a,a);
    51       b>>=1;
    52    }
    53    return res;
    54 }
    55 int main()
    56 {
    57    ll t;
    58    scanf("%I64d",&t);
    59    while(t--){
    60       scanf("%I64d%I64d",&n,&k);
    61       if(k>n){
    62          printf("0
    ");
    63          continue;
    64       }
    65       ll tmp=n-k+1;
    66       if(tmp==1){
    67          printf("1
    ");
    68          continue;
    69       }
    70       if(tmp==2){
    71          printf("2
    ");
    72          continue;
    73       }
    74       if(tmp==3){
    75          printf("5
    ");
    76          continue;
    77       }
    78 
    79       Matrix ttt;
    80       ttt.mp[0][0]=4;
    81       ttt.mp[0][1]=-4;
    82       ttt.mp[1][0]=1;
    83       ttt.mp[1][1]=0;
    84 
    85       ttt=fastm(ttt,tmp-3);
    86 
    87       Matrix cnt;
    88       cnt.mp[0][0]=5;
    89       cnt.mp[1][0]=2;
    90       ttt=Mul(ttt,cnt);
    91       printf("%I64d
    ",ttt.mp[0][0]);
    92 
    93    }
    94     return 0;
    95 }
    View Code
  • 相关阅读:
    前端周刊第一期
    Java CAS 原理详解
    【转载】Linux系统调用SYSCALL_DEFINE详解
    简述伪共享和缓存一致性MESI
    exchange发邮件
    Flutter屏幕适配(自适应)方案
    dart类初始化 future方案
    windows下postgresql自启动
    How can I call an async method in StatelessWidget.build method?
    实战分层架构
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5040432.html
Copyright © 2020-2023  润新知