[HNOI2011]数学作业 矩阵快速幂 BZOJ 2326

题目描述

小 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;
}

 


posted @ 2019-01-02 18:50  NKDEWSM  阅读(282)  评论(0编辑  收藏  举报