[easy]CodeForces-897B Chtholly's request 模拟/找规律

该博客介绍了一道来自CodeForces的897B问题,即寻找前k个长度为偶数的回文数并计算它们的和,对p取模。博主分享了解题思路,提到可以不依赖规律直接进行模拟,通过枚举整数并构造回文数,记录前缀和来求解。给出了相应的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:
http://codeforces.com/problemset/problem/897/B

Description:

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


  • 题意
  • 求从1开始数前k个的长度为偶数的回文串的和,对p取模
  • 分析
  • 这题据说可以找规律,不过看到题目数据范围觉得直接模拟就可以,利用回文数的特征,直接枚举1-1e5范围的整数,然后倒着做找出它的回文另一半,记录一下前缀和即可。
    例如:通过234->234432

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
LL sum[maxn];
int rec[25];

void Init()
{
    for(int i = 1;i <= 100000;i++)
    {
        int p = 0,tmp = i;
        while(tmp > 0){
            rec[p] = tmp % 10;
            tmp/=10;
            p++;
        }
        for(int i = p;i <= 2*p-1;i++)
            rec[i] = rec[i - p];
        for(int i = 0;i < p;i++){
            rec[i] = rec[2 * p - 1 - i];
        }
        LL ans = 0;
        for(int i = 0;i < 2*p;i++){
            ans *= 10;
            ans += rec[i];
        }
        sum[i] = sum[i - 1] + ans;
    }
}

int main()
{
    Init();
    int k,p;
    scanf("%d%d",&k,&p);
    printf("%lld\n",sum[k]%p);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值