欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-02-24 12:11:34
...

B. Chtholly's request
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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, Codeforces Round #449 (Div. 2) B Chtholly's request (预处理).




【题意】

回文串 从小到大 !!!!!!!   长度是偶数的  必须是(  开始题意没看明白, 连奇数一块算了   WA test 3   - - )


【思路】

预处理 ,处理 1e5 个就够了  


【代码实现】


#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define  FI(n) IO::read(n)
#define  Be IO::begin()

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=500005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

namespace IO {
	const int MT = 5e7;
	char buf[MT]; int c,sz;
	void begin(){
		c = 0;
		sz = fread(buf, 1, MT, stdin);//一次性输入
	}
	template<class T>
	inline bool read(T &t){
		while( c < sz && buf[c] != '-' && ( buf[c]<'0' || buf[c] >'9')) c++;
		if( c>=sz) return false;
		bool flag = 0; if( buf[c]== '-') flag = 1,c++;
		for( t=0; c<=sz && '0' <=buf[c] && buf[c] <= '9'; c++ ) t= t*10 + buf[c]-'0';
		if(flag) t=-t;
		return true;
	}
}
ll inv[maxn*2];
inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
inline ll inv2(ll b){return qpow(b,MOD-2);}

ll a[maxn];
ll rev(ll x)
{
    ll sum=0;
    ll t=x;
    while(t)
    {
       sum= sum*10+t%10;
        t/=10;
    }
    return sum;
}
void init()
{
    int k=2;
    int cot=1;
    int m;
    for(;k<=30;k+=2)
    {
       /* if(k&1)
        {
            m=(k-1)/2;
            int last=pow(10,m)-1;
            int i=pow(10,m-1);
            for(;i<=last;i++)
            {
               for(int y=0;y<=9;y++)
               {
                   a[cot++]=i*pow(10,m+1)+y*pow(10,m)+i;
               }
            }
        }
        else*/
        {
            m=k/2;
            ll last=qpow(10,m)-1;
            ll i=qpow(10,m-1);
            //cout<<last<<" "<<i<<endl;

            for(;i<=last;i++)
            {
                a[cot++]=i*qpow(10,m)+rev(i);
                if(cot>maxn)
                    return;
            }
        }
    }
    //cout<<cot<<endl;
}
int main()
{
    int k;
    ll p;
    init();

    //for(int i=1;i<=100000;i++)
       //printf("%lld \n",a[i]);
    while(~scanf("%d %I64d",&k,&p))
    {
        ll sum=0;
        for(int i=1;i<=k;i++)
        {
            sum+=a[i]%p;
        }
        printf("%I64d\n",sum%p);
    }
    return 0;
}


123

相关标签: 回文串 求和