1. 程式人生 > >[HNOI2011]數學作業 矩陣快速冪

[HNOI2011]數學作業 矩陣快速冪

題目描述

小 C 數學成績優異,於是老師給小 C 留了一道非常難的數學作業題:

給定正整數 NNN 和 MMM ,要求計算Concatenate(1..N) Concatenate (1 .. N) Concatenate(1..N) ModModMod MMM 的值,其中 Concatenate(1..N) Concatenate (1 .. N) Concatenate(1..N) 是將所有正整數 1,2,…,N1, 2, …, N1,2,,N 順序連線起來得到的數。例如,N=13N = 13N=13 , Concatenate(1..N)=12345678910111213Concatenate (1 .. N)=12345678910111213Concatenate(1..N)=12345678910111213 .小C 想了大半天終於意識到這是一道不可能手算出來的題目,於是他只好向你求助,希望你能編寫一個程式幫他解決這個問題。

輸入輸出格式

輸入格式:

從檔案input.txt中讀入資料,輸入檔案只有一行且為用空格隔開的兩個正整數N和M,其中30%的資料滿足1≤N≤10000001≤N≤10000001N1000000 ;100%的資料滿足1≤N≤10181≤N≤10^{18}1N1018 且1≤M≤1091≤M≤10^91M109 .

輸出格式:

輸出檔案 output.txt 僅包含一個非負整數,表示 Concatenate(1..N)Concatenate (1 .. N)Concatenate(1..N) ModModMod MMM 的值。

輸入輸出樣例

輸入樣例#1: 複製
13 13
輸出樣例#1: 複製
4

範圍1e18,logn演算法;
遞推方程: f[ n ]= f[ n-1 ] * 10^( len(n) )+ n;
直接遞推顯然不行;
換成矩陣加速遞推;
( fn,n,1 )= ( f(n-1),n-1,1 )( 10^(len) , 0 , 0
1 , 1 , 0
1 , 1 , 1 );
這裡的 len 指的是 n 的位數;
那麼我們每次列舉其位數進行計算即可;
這裡我的快速冪參考了別人的封裝;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

ll mode;
struct mat {
	ll n, m, a[5][5];
	mat(ll n, ll m) {
		this->n = n; this->m = m; ms(a);
	}
	mat(ll n, ll m, char e) {
		this->n = n; this->m = m; ms(a);
		for (int i = 1; i <= n; i++)a[i][i] = 1;
	}
	ll *operator[](const ll x) {
		return a[x];
	}
	mat operator*(mat b) {
		mat c(n, b.m);
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= b.m; j++) {
				for (int k = 1; k <= m; k++)
					c[i][j] = (c[i][j] + a[i][k] % mode*b[k][j] % mode) % mode;
			}
		}
		return c;
	}
	void operator*=(mat& b) {
		*this = *this*b;
	}
	mat operator ^(ll b) {
		mat ans(n, m, 'e'), a = *this;
		while (b) {
			if (b % 2)ans = ans * a;
			a *= a; b >>= 1;
		}
		return ans;
	}
};

ll n;
int getlen(ll x) {
	int len = 0;
	while (x) {
		len++; x /= 10;
	}
	return len;
}

ll pow10(int x) {
	ll ans = 1;
	for (int i = 1; i <= x; i++)ans *= 10; return ans;
}


int main() {
	//ios::sync_with_stdio(0);
	rdllt(n); rdllt(mode);
	mat A(1, 3);
	int len = getlen(n);
	A[1][1] = A[1][2] = A[1][3] = 1;
	for (int i = 0; i < len - 1; i++) {
		mat B(3,3);
		B[1][1] = pow10(i + 1) % mode;
		B[2][1] = B[2][2] = B[3][1] = B[3][2] = B[3][3] = 1;
		ll m = (pow10(i + 1) - pow10(i));
		A = A * (B ^ (m - (i == 0 ? 1 : 0)));
	}
	mat B(3, 3);
	B[1][1] = pow10(len) % mode;
	B[2][1] = B[2][2] = B[3][1] = B[3][2] = B[3][3] = 1;
	ll m = n - pow10(len - 1) + 1;
	A = A * (B ^ (m - (len - 1 == 0 ? 1 : 0)));
	cout << (ll)A[1][1] << endl;
	return 0;
}