-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler036.cpp
47 lines (44 loc) · 802 Bytes
/
Euler036.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool palin(int n){
int m=n,rn=0;
while(m){
rn=rn*10+m%10;
m/=10;
}
if(n==rn)
return 1;
return 0;
}
bool base(int n,int k){
string ans="";
while(n){
ans.push_back(n%k + 48);
n/=k;
}
n=ans.size();
for(int i=0;i<n/2;i++){
if(ans[i]!=ans[n-i-1])
return 0;
}
return 1;
}
int main() {
int t,k;
cin>>t>>k;
long long sum=0;
for(int i=1;i<=t;i++){
if(palin(i)){
if(base(i,k)){
sum+=i;
}
}
}
cout<<sum<<endl;
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}