Codeforces Round #449 (Div. 2) B. Chtholly's request

来源:互联网 发布:期货软件哪个好 编辑:程序博客网 时间:2024/06/15 01:48
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 ispalindrome 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~k,就是回文数的一半,比如1,2,3,4是(11,22,33,44)的一半,所以长度为偶数,我们只用枚举它的前一半。然后就是求它的另外一半,求一半的时候可以注意规律,例如1221=(12*10+12%10))*10+(12/10)%10。注意数据可能会超过int所以要用long long。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
long long int sum=0;
int main()
{
    int k,p,j;
    scanf("%d %d",&k,&p);
    for(int i=1; i<=k ;++i)
    {
        j=i;
        int a;
        long long int temp=i;
        while(j)
        {
            a=j%10;
            temp=temp*10+a;
            j/=10;
        }
        sum+=temp;
    }
    printf("%d\n",sum%p);
    return 0;
}

阅读全文
0 0