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

题意:回文数求和取模,回文数必须满足:①偶数位数②回文数。输入k求前k项的回文数之和模p,(1<=k<=10^5)

思路:刚开始是直接去求回文数,那么肯定超时,之后找了一下规律,发现可以使用。求前k项之和,其实就是1,2,3,4......120.....k.....的于自己构成回文数之和(11,22,33,44.....120021......kk')

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[100005];
LL change(int n)
{
    int t=n,num=1;
    LL ans=0;
    while(t)
    {
        ans=ans*10+t%10;
        t=t/10;
        num=num*10;
    }
    ans=ans+(LL)n*(LL)num;
    return ans;
}

int main()
{
    for(int i=1;i<=100000;i++)
    {
        a[i]=change(i);
    }
    LL  k,p;
    cin>>k>>p;
    LL ans=0;
    for(int i=1;i<=k;i++)
    {
        ans=(ans+a[i])%p;
    }
    cout<<ans<<endl;
    /*int x;
    while(cin>>x)
    {
        cout<<"a["<<x<<"]="<<a[x]<<endl;
    }*/
    return 0;
}
View Code

 

posted @ 2017-12-03 11:28  孟加拉国  阅读(73)  评论(0编辑  收藏  举报