1. 程式人生 > >codeforces-div2-449-B

codeforces-div2-449-B

fine def turn efi ace codeforce ios 回文 force

題意:確定一個回文偶數十進制數字,輸入k和q,求前k小的和對q取余的值

解題思路:首先確定一個,第k個回文偶數一定前半段一定是k,比如第12個,這個數就是1221;

代碼:

#include<iostream>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int main()
{
ll k,q;
ll sum;
ll temp;
ll x1;
ll x2;
int a[15];
int cnt;
cin>>k>>q; sum=0;
while(k!=0)
{
cnt=0;
temp=0;

int m=k;
while(m)
{
a[++cnt]=m%10;
m=m/10;
}
x1=1;x2=1;
for(int i=1;i<=cnt;i++)
x2=x2*10;
//cout<<x2<<endl;
for(int i=cnt;i>=1;i--)
{
temp+=x1*a[i];
x1*=10;
}
// cout<<temp<<endl;
for(int i=1;i<=cnt;i++)
{
temp+=x2*a[i];
x2*=10;
}
//cout<<temp<<endl;
temp=temp%q;
sum+=temp;
sum=sum%q;
k--;
}
cout<<sum<<endl;
return 0;
}

codeforces-div2-449-B