CF-897B.Chtholly's request

来源:互联网 发布:淘宝开发者平台 api 编辑:程序博客网 时间:2024/05/24 03:05

Chtholly’s request

— 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
题意:位数为偶数并且回文的数为zcy数,求zcy数前k项和取模p。
zcy数就是类似:11,22,33,44……。
这道题难点在于找到zcy数。
题解:第i个zcy数就是ii(反序),所以这里就应该想到把整数变成字符串,然后再对字符串进行操作,(由于是初学者对字符串操作不是很熟练),然后再把字符串变成整数。
附上字符串操作总结的链接:http://www.jb51.net/article/3740.htm
以上链接只有函数名称和作用,具体用法还要自己百度。
这次要用到的函数有:
1.sprintf函数:字串格式化命令,主要功能是把格式化的数据写入某个字符串中(通俗的来说就是将任意类型数据写入字符串)。
2.strrev函数:将字符串反转。
3.strdup函数:将串拷贝到新建的位置处。strdup()在内部调用malloc()为变量分配内存,不需要使用返回的字符串时,需要用free()释放相应的内存空间,否则会造成内存泄漏。(用这个函数主要是因为strcat函数要求两个字符串名称所指内存区域不可重叠,用strrev函数反转字符串后其所指内存区域与原串重叠,会出错。)
4.strcat函数:strcat是连接字符串的函数。函数返回指针,两个参数都是指针,第一个参数所指向的内存的地址必须能容纳两个字符串连接后的大小。
5.atoll函数:将字符串转换为长整形。(atol-数据类型的第一个字母,就可以转换成对应的数据。)(例子:将123456这个字符串转换成123456这个数字。)
代码如下(这题数据太大必须用long long int):

#include<bits/stdc++.h>using namespace std;int main(){    int k;    long long int  p;    scanf("%d%lld",&k,&p);        long long int a[100000+5],d=0,da;        int  i;        char b[200000+5];        char b1[100000+5];         for(i=1;i<100002;i++)        {            sprintf(b,"%d",i);            sprintf(b1,"%s",strrev(strdup(b)));            strcat(b,b1);            a[i]=atoll(b);            d+=a[i];            if(i==k)            {                da=d%p;                printf("%lld\n",da);                break;            }        }    return 0;}

完全就是在考字符串操作!!!

原创粉丝点击