• 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. }

     

  • 相关阅读:
    vscode远程连接XHR(wget download failed)解决方法
    小程序地图相关
    人脸识别5.1.3- insightface人脸识别模型arcface-Paddle
    vue3中defineComponent 的作用
    文献速递:GAN医学影像合成--用生成对抗网络生成 3D TOF-MRA 体积和分割标签
    【数字IC前端入门】03-Linux基础操作(持续更新....)
    Linux 基本语句_6_C语言_文件操作
    HR领域有哪些含金量高的证书?这个证书了解一下!
    你真的了解黑客吗?
    EOCR-3E420,3EZ,3DE电机保护器与变频器配合使用的方法
  • 原文地址:https://blog.csdn.net/q619718/article/details/127771764