HDU-5793 A Boring Question(打表找规律)

来源:互联网 发布:linux重启命令init 编辑:程序博客网 时间:2024/05/22 17:09

题意:

求(∑0≤k1,k2...km≤n(1≤j<m C(kj+1, kj)))%1000000007的值;

思路:

打个表很容易就找到ans[n] = m*ans[n-1]+1;


代码:

#include <algorithm>  #include <string.h>  #include <cstdio>    #define LL long long    using namespace std;    const LL mod = 1e9+7;struct node  {      LL m[2][2];  };  int n, m;node multi(node x, node y)  {      node res = {0, 0,                  0, 0};      for(int i = 0; i < 2; ++i)      for(int j = 0; j < 2; ++j)      for(int k = 0; k < 2; ++k)          res.m[i][j] += x.m[i][k]*y.m[k][j], res.m[i][j] %= mod;      return res;  }  LL qpow(LL n)  {      node ans = {1, 0,                   0, 1};      node bas = {m%mod, 1,                 0, 1};      node col = {1, 0,                 1, 0};      while(n)      {          if(n&1) ans = multi(ans, bas);          bas = multi(bas, bas);          n >>= 1;      }      return multi(ans, col).m[0][0];  }   int main()    {        int t;      scanf("%d", &t);        while(t--)        {            scanf("%d %d", &n, &m);            if(n == 0) {puts("1"); continue;}          printf("%lld\n", qpow(n));       }        return 0;    }


继续加油~

原创粉丝点击