A - Chtholly's request CodeForces - 897B

A - 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.
Example
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, (11+22+33+44+55)mod 30=15;
题意:对于长度为偶数并且为回文数的数叫zcy数,求最小的k个zcy数和对p求模的结果

#include<iostream>
using namespace std;
bool zcy(int a)   //判断是否为zcy数
{
    int n=a,cnt=0;
    int newd=0;
    do
    {
        newd=newd*10+n%10;
        n/=10;
        cnt++;
    }while(n>0);
    if(newd==a&&cnt%2==0)
        return true;
    else
        return false;
}

int main()
{
    int k,p,sum=0;
    int i=0;
    cin>>k>>p;
    while(k>0)
    {
        if(zcy(i++))
        {
            k--;
            sum+=(i-1);
        }
    }
    cout<<sum%p<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weifuliu/article/details/78907983