Codeforces Round #449 (Div. 2) B. Chtholly's request

来源:互联网 发布:访客统计系统源码 编辑:程序博客网 时间:2024/06/05 19:28
B. Chtholly's requesttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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!InputThe first line contains two integers k and p (1 ≤ k ≤ 105, 1 ≤ p ≤ 109).OutputOutput single integer — answer to the problem.Examplesinput2 100output33input5 30output15NoteIn 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)%30=15.

题目大意:
给你一个k和p,求前k个偶数数字回文的和并对p余。这个题唯一的坑点就在于1e5时它最大的数据是1000000001,所以单纯的打表是会超时的。而这里队友的思路就把这个题目简单化了。你只需求前1e5的偶数数字回文,再加上(它乘pow(10,它本身的位数)+它的逆置)然后不断累加前k个 就得得到了最终答案

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <bits/stdc++.h>using namespace std;const long long N=1e5+1;const int M=3e5+10;long long s[M];int flag[M];long long f(long long n) //求逆置{    long long k=0,m;    m=n;    while(m)    {        k=k*10+m%10;        m/=10;    }    return k;}int fff(long long n) //判断是否是回文{    long long k;    k=f(n);    if(k==n)        return 1;    else        return 0;}long long ff(long long n) //判断位数{    long long k=0;    while(n/10>0)    {        k++;        n/=10;    }    k++;    return k;}int main(){    ios::sync_with_stdio(false);    long long k,p;   long long i;   int j=0,tmp=0;    memset(s,0,sizeof(s));    while(cin>>k>>p)    {        long long ans=0;        for( i=1; i<=k; i++)        {            ans=(ans+i*(long long)pow(10,ff(i))+f(i))%p;        }        cout<<ans<<endl;    }    return 0;}
原创粉丝点击