Codeforces 897 B.Chtholly's request-思维题(处理前一半)

 
 
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
— Thanks a lot for today.

— I experienced so many great things.

— You gave me memories like dreams... But I have to leave now...

— One last request, can you...

— Help me solve a Codeforces problem?

— ......

— What?

Chtholly has been thinking about a problem for days:

If a number is palindrome and length of its decimal representation without leading zeros is even, we call it a zcy number. A number is palindrome means when written in decimal representation, it contains no leading zeros and reads the same forwards and backwards. For example 12321 and 1221 are palindromes and 123 and 12451 are not. Moreover, 1221 is zcy number and 12321 is not.

Given integers k and p, calculate the sum of the k smallest zcy numbers and output this sum modulo p.

Unfortunately, Willem isn't good at solving this kind of problems, so he asks you for help!

Input

The first line contains two integers k and p (1 ≤ k ≤ 105, 1 ≤ p ≤ 109).

Output

Output single integer — answer to the problem.

Examples
input
2 100
output
33
input
5 30
output
15
Note

In the first example, the smallest zcy number is 11, and the second smallest zcy number is 22.

In the second example, .

 

题意就是回文,而且回文的长度是偶数,将前k个符合条件的加起来然后%p。

直接将这个回文分成两半,将前一半的反转一下就是后面一半的。

 

代码:

 1 //B.Chtholly's request 处理前一半
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 typedef long long ll;
 9 const int N=1e5+10;
10 int h;
11 ll s[N];
12 int zz(int n){
13     int a[10];
14     while(n!=0){
15         a[h++]=n%10;
16         n/=10;
17     }
18     int ans=0;
19     for(int i=0;i<h;i++){
20         ans=ans*10+a[i];
21     }
22     return ans;
23 }
24 int main(){
25     int k,p;
26     ll ans;
27     while(~scanf("%d%d",&k,&p)){
28         for(int i=1;i<=k;i++){
29             h=0;
30             int x=zz(i);
31             s[i]=i*pow(10,h)+x;
32         }
33         ans=0;
34         for(int i=1;i<=k;i++){
35             ans=(ans+s[i])%p;
36         }
37         printf("%lld\n",ans);
38     }
39     return 0;
40 }

 

posted @ 2017-12-06 21:40  ZERO-  阅读(265)  评论(0编辑  收藏  举报