LJX Campus: Entrance Ceremony (c + +)

Source: Internet
Author: User

LJX's campus: entrance ceremony
Difficulty level: C; operating time limit: 45ms; operating space limit: 256000KB; code length limit: 2000000B
Question Description

LJX in elementary School! He and YSM,YSF,WHT,LTJ and others are alumni. Today is a day of drowning in his life. Today, he will prove to his classmates that his maths is "happy". So, just learn simple a+b problem He, in class, to enemies Swatch Challenge Qaq, Swatch team have yzm,sjy,zzq and other people. And the LJX team has his good friends (JI) Friends: Ysm,ysf,wht,ltj,lzh and other people, the strength of the weak underestimated. There is one such problem:

Given a positive integer n,m, the computational 1,2,......,n is required to be connected (1234567891011 ...). N) The value of mod m.

"Recruit" Ljx thought, if M is 3, or 9, he will. But M can do anything, as long as it is less than INF. After the "appointment" of the various methods, the brain can't think out. So, right turned to (hehe) his (ZHU) teammates. But they had already run TAT. Jue Ruo LJX found you, he knelt and begged you to compile a program to help him solve the problem. Otherwise, he will "exercise" at the graduation ceremony and be qaq by the horrible swatch.

Input
* One line: positive integer n,m.
Output
* One line: output as required
Input example
Input Example 1
13 13
Input Example 2
12345678910 1000000000
Output example
Output Example 1
4
Output Example 2
345678910
Other Notes
N<=10^18
M<=10^9
This data is in the pit LJX AH

Use today's matrix to learn the fast power
Oh, no, it's a fast power for piecewise matrices.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

#define MAXN 4

using namespace Std;

typedef long long int LL;

int mod;

struct matrix
{
LL P[MAXN][MAXN];
}ans,tmp;

Matrix operator* (Matrix A,matrix B)
{
Matrix C;
for (int i=1;i<=3;i++)
for (int j=1;j<=3;j++)
{
c.p[i][j]=0;
for (int k=1;k<=3;k++)
C.p[i][j]= (c.p[i][j]+ (a.p[i][k]%mod) * (b.p[k][j]%mod)%mod)%mod;
}
return C;
}

void Cal (LL t,ll last)
{
memset (tmp.p,0,sizeof (TMP.P));
tmp.p[1][1]=t;
Tmp.p[2][1]=tmp.p[3][1]=tmp.p[2][2]=tmp.p[3][2]=tmp.p[3][3]=1;
LL y=last-t/10+1;
while (y)
{
if (y&1) ans=ans*tmp;
tmp=tmp*tmp;
y>>=1;
}
}

int main ()
{
for (int i=1;i<=3;i++)
Ans.p[i][i]=1;
LL N;
scanf ("%lld%lld", &n,&mod);
LL t=10;
while (n>=t)
{
Cal (T,t-1);
t*=10;
}
Cal (T,n);
printf ("%lld\n", ans.p[3][1]);
return 0;
}

LJX Campus: Entrance Ceremony (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.