Kaleidoscopic Palindromes(2018 ACM-ICPC North Central North America Regional Contest)
Nicholas Neverson was a student at Northlings Neverland Academy. As with any daydreaming student, Nicholas was playing around with a Kaleidoscope one day instead of paying attention to the teacher. Since this was math class, his daydreams quickly turned to palindromic numbers. A palindromic number is any number which reads the same forwards and backwards.
He describes his vision to you at lunch: numbers which are palindromic in several bases at once. Nicholas wonders how many such numbers exist. You decide you can quickly code up a program that given a range and a number kk, outputs the number of numbers palindromic in all bases jj, 2≤j≤k2≤j≤k, in that range.
Input
Input consists of three space-separated integers: aa, bb, and kk. The input satisfies the following constraints:
0≤a≤b≤2000000,2≤k≤100000.0≤a≤b≤2000000,2≤k≤100000.
Output
Output the quantity of numbers between aa and bb inclusive which are palindromes in every base jj, for 2≤j≤k2≤j≤k.
Sample Input 1 | Sample Output 1 |
---|---|
1 356 2 |
36 |
Sample Input 2 | Sample Output 2 |
---|---|
18 118 13 |
0 |
題意:在a-b內,j從2-k內j進位制如果都是迴文則++;
#include<stack>
#include<iostream>
#include<stdio.h>
#include<string.h>
#define ll long long
using namespace std;
int num[200];
int hw(int a[],int n)
{int i,j;
for( i=0, j=n-1; j>i; j--,i++)
{
if(a[i]!=a[j])
return 0;
}
return 1;
}
int zh(int a,int b)
{
stack<int>s;
int c;
while(a)
{
c=a%b;
s.push(c);
a=a/b;
}
int x=0;
while(!s.empty())
{
num[x++]=s.top();
s.pop();
}
return x;
}
int main()
{
int a,b,k,x;
cin>>a>>b>>k;
int ans=0;
for(int i=a; i<=b; i++)
{
int f=1;
for(int j=2; j<=k; j++)
{
memset(num,0,sizeof(num));
x=zh(i,j);
if(hw(num,x)==0)
{
f=0;
break;
}
}
if(f)
ans++;
}
printf("%d\n",ans);
return 0;
}