概述
B. Chtholly’s request
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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的取值达到105次方,若暴力,从1开始累加再判断肯定超时,但我们可以看到题目只要求位数是偶数的数,因此,根据回文数的特征,比如123321,1221,前一半的数会等于后一半数的倒置。
当n=1时,所得回文数是11;
当n=2时,所得回文数是22;
…
…
当n=11时,所得回文数是1111;
不难发现回文数的大小和n的取值有关,当n1<n2时,所得回文数M1<M2,根据这个特征,我们便很容易找出前k个回文数,再进行相加,因为第105 个回文数会十分大,再加上sum会超出int范围,因此,在每次相加过程都必须对sum取模。
代码如下:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int ss(int d){
int num = 0;
while(d){
d /= 10;
num++;
}
return num;
}
int xx(int d,int num){
int s=0;
int j=num;
while(d){
int t = d % 10;
s += t * pow(10.0,num-1);
d /= 10;
num --;
}
return s;
}
int main(int argc, char *argv[]) {
int k,p;
while(~scanf("%d%d",&k,&p)){
long long sum = 0;
for(int i=1; i<=k; ++i){
int num = ss(i);
sum += i * pow(10.0,num);
sum %= p;
sum += xx(i,num);
sum %= p;
}
printf("%lldn",sum);
}
}
最后
以上就是长情树叶为你收集整理的Codeforces Round #449 (Div. 2).B的全部内容,希望文章能够帮你解决Codeforces Round #449 (Div. 2).B所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复