矩阵快速幂求调
查看原帖
矩阵快速幂求调
571841
ZVitality楼主2024/3/22 19:52

rt,样例都过不去,每次矩阵跟随机数一样,但是找不到错误。按照第一篇构造的矩阵。

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int _ = 5;
int n,Mod;

template<int maxx>
struct mat {
  int a[maxx][maxx];
  int n;
  void clear(){ memset(a,0,sizeof(a)); }
  void resize(int nn) { n = nn; }
  mat operator*(const mat &x) {
    mat<_> res;
    res.resize(n);
    for(int k = 1;k <= n;k++) {
      for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= n;j++) {
          res.a[i][j] = (res.a[i][j] + a[i][k] * x.a[k][j]) % Mod;
        }
      }
    }
    return res;
  }
};

mat<_> ans,bas;

signed main() {
  cin >> n >> Mod;
  ans.clear(),bas.clear();
  ans.resize(3),bas.resize(3);
  ans.a[1][3] = 1;
  for(int x = 1;x <= n;x *= 10) {
  	bas.clear();
  	bas.a[1][1] = x * 10;
  	bas.a[2][3] = -1;
	bas.a[3][1] = bas.a[3][2] = 1;
	bas.a[3][3] = 2;
  	int b = min(x * 10 - 1,n) - x + 1;
  	while(b) {
  	  if(b & 1) ans = ans * bas;
  	  bas = bas * bas;
  	  b /= 2;
	}
	cout << ans.a[1][1] << ' ' << ans.a[1][2] << ' ' << ans.a[1][3] << '\n';
  }
  return 0;
}

/*
dp[i]=(dp[i-1]*10^{digit(i)}+i)%m
dp[i] i i+1->dp[i+1] i+1 i+2
x 0 0
0 0 -1
1 1 2
*/
2024/3/22 19:52
加载中...