449B

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, .

                      输入n和m,得到前n个长度是偶数的回文数的和 sum,输出sum模上m的值,

  本来用了暴力去解,    但是爆掉了,本来我是从1开始检测,一直检测到第n个符合条件的数结束,

但是测试数据里有这样一组数

42147 412393322
#include<stdio.h>
#include<string.h>
int main()
{
 int m,n,i,t,j,h,len,k,d[100000],a,b;
 long long sum;
 char p[11];
 h=1;
 for(i=11;i<=100000;i++)
  {
   k=0;
   d[1]=0;
   sprintf(p,"%d",i);
   len=strlen(p);
   if(len%2==0)
   {
  
    for(j=0;j<(len+1)/2;j++)
       if(p[j]!=p[len-j-1])
       {k=1;break;}
       if(k==0)
       {
  
        d[h+1]=d[h]+i;
        h++;  
       }  
    }d[i]=d[h]; 
    
  }
 while(scanf("%d%d",&n,&m)!=EOF)
 {
  printf("%d\n",d[n+1]%m);
    }
}

AC的代码:

#include<stdio.h>
int main()
{
 int m,a,b,i,j;
 long long n,t,t1,sum;
 while(scanf("%lld%d",&n,&m)!=EOF)
 {
  sum=0;
  for(i=1;i<=n;i++)
  {
   t=t1=i;
   while(t!=0)
   {
   t1=t1*10+t%10;
   t/=10;
   }
   sum=(sum+t1)%m;
  }
  
  printf("%lld\n",sum);
 }
}
原文地址:https://www.cnblogs.com/wenbo4399/p/7991902.html