• D - Magic Gems


    Reziba has many magic gems. Each magic gem can be split into MM normal gems. The amount of space each magic (and normal) gem takes is 11 unit. A normal gem cannot be split.

    Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is NN units. If a magic gem is chosen and split, it takes MM units of space (since it is split into MM gems); if a magic gem is not split, it takes 11 unit.

    How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is NN units? Print the answer modulo 10000000071000000007 (10^9+7109+7). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ.

    Input

    The input contains a single line consisting of 22 integers NN and MM (1 \le N \le 10^{18}1≤N≤1018, 2 \le M \le 1002≤M≤100).

    Output

    Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is NN units. Print the answer modulo 10000000071000000007 (10^9+7109+7).

    Sample 1

    InputcopyOutputcopy
    4 2
    
    5
    

    Sample 2

    InputcopyOutputcopy
    3 2
    
    3
    

    Note

    In the first example each magic gem can split into 22 normal gems, and we know that the total amount of gems are 44.

    Let 11 denote a magic gem, and 00 denote a normal gem.

    The total configurations you can have is:

    • 1 1 1 11111 (None of the gems split);
    • 0 0 1 10011 (First magic gem splits into 22 normal gems);
    • 1 0 0 11001 (Second magic gem splits into 22 normal gems);
    • 1 1 0 01100 (Third magic gem splits into 22 normal gems);
    • 0 0 0 00000 (First and second magic gems split into total 44 normal gems).

    Hence, answer is 55.

    1. #include <iostream>
    2. #include <algorithm>
    3. #include <cstring>
    4. using namespace std;
    5. const int MOD=1e9+7;
    6. template <unsigned int N>
    7. struct Matrix {
    8. unsigned int size = N;
    9. long long a[N][N];
    10. Matrix() { memset(a, 0, sizeof a); }
    11. Matrix operator*(const Matrix &b) const {
    12. Matrix res;
    13. for (unsigned i = 1; i < N; ++i)
    14. for (unsigned j = 1; j < N; ++j)
    15. for (unsigned kk = 1; kk < N; ++kk)
    16. res.a[i][j] = (res.a[i][j] + a[i][kk] * b.a[kk][j] % MOD) % MOD;
    17. return res;
    18. }
    19. void setE(int x) {
    20. for (unsigned i = 1; i < N; ++i)
    21. a[i][i] = x;
    22. }
    23. };
    24. template <unsigned int N> Matrix<N> qpow(Matrix<N> m, long long n) {
    25. Matrix<N> ans;
    26. ans.setE(1);
    27. while (n > 0) {
    28. if (n & 1)
    29. ans = ans * m;
    30. m = m * m;
    31. n >>= 1;
    32. }
    33. return ans;
    34. }
    35. int main(){
    36. long long n,m;
    37. scanf("%lld%lld",&n,&m);
    38. Matrix<105>a,f;
    39. for(int i=1;i<m;i++)a.a[i][1]=1;
    40. a.a[m][1]=1;
    41. f.a[1][1]=f.a[1][m]=1;
    42. for(int i=1;i<=m;i++){
    43. f.a[i+1][i]=1;
    44. }
    45. if(n<m){
    46. printf("1\n");
    47. }
    48. else{
    49. Matrix<105> ans=qpow(f,n-m+1)*a;
    50. printf("%lld\n",ans.a[1][1]);
    51. }
    52. }

     

  • 相关阅读:
    python常用命令
    使用HTTP代理上网安全吗?
    IOday6
    根文件系统制作并启动 Linux
    nginx-location和proxy_pass的url拼接
    瑞吉外卖项目实战Day01
    树莓派无需显示屏的VNC Viewer方式的远程连接
    16.添加脚注footnote
    LeetCode-201. 数字范围按位与-Java-medium
    打造“新型计算数据中心”!玻色量子与科华数据(002335.SZ)携手共创
  • 原文地址:https://blog.csdn.net/q619718/article/details/127771764