1. 程式人生 > >bzoj2326(dp+矩陣快速冪)

bzoj2326(dp+矩陣快速冪)

這個化成dp[i]=dp[i-1]*bit+i就可以了,bit為10^位數。。

然後bit看起來不好轉移。。不過bit最多為18,可以分類列舉一下。。

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神獸保佑,程式碼無bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<assert.h>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 300005
#define nm 200005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



ll inf,_x,n,_y;

struct mat{int n,m;ll a[4][4];}t,ans,E;
mat operator*(const mat&x,const mat&y){
    mat s;s.n=x.n;s.m=y.m;mem(s.a);
    inc(i,1,s.n)inc(j,1,s.m)inc(k,1,x.m)s.a[i][j]+=x.a[i][k]*y.a[k][j]%inf,s.a[i][j]%=inf;
    return s;
}
mat qpow(mat x,ll t){return t?qpow(sqr(x),t>>1)*(t&1?x:E):E;}


int main(){
    E.a[1][1]=E.a[2][2]=E.a[3][3]=1;E.n=E.m=3;
    n=read();inf=read();
    if(n==1)return 0*printf("1\n");
    t.n=t.m=3;ans.n=3;ans.m=1;
    t.a[2][3]=t.a[2][2]=t.a[1][2]=t.a[1][3]=t.a[3][3]=1;
    ans.a[1][1]=ans.a[2][1]=0;ans.a[3][1]=1;
    //inc(i,1,t.n){inc(j,1,t.m)printf("%d ",t.a[i][j]);putchar('\n');}
    //inc(i,1,E.n){inc(j,1,E.m)printf("%d ",E.a[i][j]);putchar('\n');}
    //inc(i,1,ans.n){inc(j,1,ans.m)printf("%d ",ans.a[i][j]);putchar('\n');}
    for(_x=1,_y=1;_y<18&&_x*10<=n+1;_y++,_x*=10){
	t.a[1][1]=_x*10%inf;
	ans=qpow(t,_x*9)*ans;
	//printf("ans:\n");inc(i,1,ans.n){inc(j,1,ans.m)printf("%d ",ans.a[i][j]);putchar('\n');}
    }
    if(n>=_x){
	t.a[1][1]=_x*10%inf;
	n=n-_x+1;
	ans=qpow(t,n)*ans;
    }
    //printf("ans:\n");inc(i,1,ans.n){inc(j,1,ans.m)printf("%d ",ans.a[i][j]);putchar('\n');}
    return 0*printf("%lld\n",ans.a[1][1]);
}

2326: [HNOI2011]數學作業

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2657  Solved: 1557
[Submit][Status][Discuss]

Description

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

要求計算 Concatenate (1 .. N) Mod M 的值,其中 Concatenate (1 ..N)是將所有正整數 1, 2, …, N 順序連線起來得到的數。

例如,N = 13, Concatenate (1 .. N)=12345678910111213.小C 想了大半天終於意識到這是一道不可能手算出來的題目,

於是他只好向你求助,希望你能編寫一個程式幫他解決這個問題。

Input

只有一行且為用空格隔開的兩個正整數N和M,

1≤N≤10^18且1≤M≤10^9.

Output

僅包含一個非負整數,表示 Concatenate (1 .. N) Mod M 的值。

Sample Input

13 13

Sample Output

4

HINT

Source