#include<bits/stdc++.h> usingnamespace std; using ll = longlong;
constint maxsz = 3;
ll mod = 10007; int k, m;
// 矩阵快速幂模板 // 这里简单说一下怎么用,矩阵的具体大小用 const int maxsz 来定义,比如说这题的矩阵是 2 * 2 // 但是下标是从 0 开始,所以 maxsz = 3;然后如何创建一个矩阵?比如说 Matrix T( 2 ); // 就是新建一个矩阵,数据类型为矩阵结构体,变量名为 T,参数,也就是大小为 2 // 如果你想来了解这些语句具体的含义,可以查 AI,因为篇幅原因就不在这里展开了。 // ---------- structMatrix { ll m[maxsz][maxsz]; int sz; Matrix( int s = 0 ) : sz( s ) { memset( m, 0, sizeof( m ) ); } voidinit( ){ for( int i = 1; i <= sz; ++ i ) m[i][i] = 1; }
friend Matrix operator * ( const Matrix &a, const Matrix &b ) { Matrix res( a.sz ); for( int i = 1; i <= res.sz; ++ i ) { for( int k = 1; k <= res.sz; ++ k ) { if( a.m[i][k] == 0 ) continue; for( int j = 1; j <= res.sz; ++ j ) res.m[i][j] = ( res.m[i][j] + a.m[i][k] * b.m[k][j] ) % mod; } } return res; } };
// 快速幂 - 矩阵版本 Matrix mqpow( Matrix a, ll k ) { Matrix res( a.sz ); res.init( ); for( ; k; k >>= 1, a = a * a ) if( k & 1 ) res = res * a; return res; } // ----------
voidsolve( ) { cin >> k >> m;
// 这里就是我们通过数论推导的大模数 mod = mod * m;
ll res = 0; for( int i = 1; i <= k; ++ i ) { ll c, l; cin >> c >> l;
// 这里是转移矩阵构建 Matrix T( 2 ); T.m[1][1] = 10, T.m[1][2] = c; T.m[2][1] = 0, T.m[2][2] = 1; // 矩阵快速幂完成递推 T = mqpow( T, l ); // 更新答案矩阵,因为第二个值是固定的 1,所以我这里就偷懒直接更新这个 X 了 res = ( T.m[1][1] * res + T.m[1][2] * 1 ) % mod; } cout << res / m << '\n'; }