天天看點

【ACM】Chtholly's request (codeforces 897B)

題面:

— 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.

思路:

需要求前k個偶數回文數的和,那麼可以進行一次預處理,求出前k個偶數回文數,即求出前1~k這k個數的回文數(eg.1052—>10522501)

#include<bits/stdc++.h>
using namespace std;
int k,p;
int hws[];
void first_deal_with(){//預處理出前個偶數回文數
    int t=;
    for(int i=;    i<=; i++){
        long long temp=i;
        int q=i;
        while(q){
            temp=temp*10+q%10;
            q=q/;
        }
        t++;
        hws[t]=temp%p;
    }
}
int main(){
    scanf("%d%d",&k,&p);
    first_deal_with();
    int tot=;
    for(int i=;    i<=k;   i++)
        tot=(tot%p+hws[i]%p)%p;
    printf("%d\n",tot%p);
    return ;
}
           

https://paste.ubuntu.com/26488740/