Palindromic NumbersProblem code: K2 |
All submissions for this problem are available.
The Chef has figured out that there are some numbers which have an interesting property: they are the same when read from left to right, and from right to left. For example, 5115 and 929 are such numbers, while 31 and 125 are not. He calls such numbers palindromic numbers.
After a while, the Chef has realized that his definition of palindromic numbers is not really precise. Whether a number is palindromic or not depends on the base in which the number is written. For example, 21 is not palindromic in base 10 but it is palindromic in base 2 (because 21 = 101012).
The Chef finds it interesting that any number becomes palindromic when it is written in an appropriate base.
Given a number N, write a program to help The Chef compute the smallest base B such that N is palindromic when written in base B.
Input
The first line contains t, the number of test cases (about 1000). Then t test cases follow.
Each test case consists of a number N written in one line (1 <= N <= 1010).
Output
For each given number N, print a line containing the smallest base B such that N is palindromic in base B.
Example
Input:
3
1
4
21
Output:
2
3
2
| Date: | 0000-00-00 |
| Time limit: | 3s |
| Source limit: | 50000 |
| Languages: | C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK TEXT JS |
Comments

Fetching successful submissions

When i try to submit, it says
When i try to submit, it says "contest_problem_id field is required.".
Please help
if it is not a palindrome thn
if it is not a palindrome thn wht shuld be the answer?
there are no such numbers !
there are no such numbers ! all are palindromes with some base.
i m getting run time error
i m getting run time error wht can be the problem it is workin well within the given limits?
mine, time limit exceeding !
mine, time limit exceeding !
can any1 put some more test
can any1 put some more test cases?
Every number is palindromic
Every number is palindromic in base 1!
Here is another test case.
I am sorry i missed a test
I am sorry i missed a test case in input
can you put some bigger
can you put some bigger values?my code is working correcly for the above cases
d output shud b like this
d output shud b like this na
3
1
2
4
3
21
2
am i crct na pls tell
am i crct na pls tell
admin some more cases some
admin some more cases some big 1
Please do not post sample
Please do not post sample test cases.
@aniruddha PLS tell for d
@aniruddha PLS tell
for d case u hv given in the ques... d output shud b like this na
3
1
2
4
3
21
2
i.e one per line or entire output after tking all the input??????
PLS tell
@ Anirudda, sorry i din know
@ Anirudda, sorry i din know thats forbidden
what is the output for input
what is the output for input n = 10000000439 ?
mine is comin 2266 wht urs?
mine is comin 2266 wht urs?
yes mine too, but actually i
yes mine too, but actually i wanted to know n = 1000000439 ? it was my mistake in previous post to add a extra zero.
please tell me what is the output for this input ?
Guys, posting test cases and
Guys, posting test cases and asking for help for solving a problem is forbidden in running contest. You might be disqualified for this
http://blog.codechef.com/2009/07/29/rule-change-for-august-contest-guide...
1(7 0's)439 in a sec 2266 1(6
1(7 0's)439 in a sec 2266
1(6 0's)439 its taking lot of time for my code..
the time limit of 3 sec is
the time limit of 3 sec is for each test case or for all the test cases (limit of 1000)
Admin can you please answer
Admin can you please answer my question of time limit
@Giri For all 1000. @All :
@Giri For all 1000.
@All : PLEASE DON'T POST TEST CASES. IT MIGHT LEAD TO DISQUALIFICATION.
but u din answer d question
but u din answer d question dat al r palindroms for base 1.. n dats d smallest right!!
i have compelted my progam
i have compelted my progam and getting perfect output for whatever input i give still the compiler of codechef is saying wrong answer
can any body help me out
No.
No.
i am gettting runtime error
i am gettting runtime error in python.... but its runing fine on my system,
upto what base we need to
upto what base we need to compute.
will every input be a pallindrome for any of the base....
The problem statement clearly
The problem statement clearly explains this.
hello friends,i'm new to
hello friends,i'm new to codechef.this is the first time i'm participating in the challenge.can any one pls tell me if i need to give the inputs within the program?if not then as i'm using java ,so i need to know if the inputs will be given in this way 3 1 4 21,which is readable from command line.
@Aniruddha (Codechef): i am
@Aniruddha (Codechef):
i am gettting runtime error in python.... but its runing fine on my system,
@ALL, Codechef:
Why its giving runtime error for python, its happens for nov contest Also, But same code written in C is accepted in nov, But not in Python(Giving run time error)
@Jitendra Please check if you
@Jitendra Please check if you are conforming to the I/O specifications mentioned. Take a look at some of the solutions in python for earlier contest problems to find out where you are going wrong.
@suvronil The input will be given from stdin and you have to write the output to stdout.
To Michael LeKander no number
To Michael LeKander
no number could be represented in base 1 except 0
because all representations in base 1 is zero
Has the time limit been
Has the time limit been lengthened? I notice in the successful submissions list , the user sppraveen with a time of 3.85 .
For solutions in java , its
For solutions in java , its time limit * 2
getting frustrated with
getting frustrated with runtime errors.but working fine on my machine.cant we see the errors occurring?otherwise it'll be difficult to solve the prob.
No, a good problem solver
No, a good problem solver should be able to and is expected to work that out for him/herself.
wat all bases do we hav to
wat all bases do we hav to consider ?
Hello Friends, I am new to
Hello Friends,
I am new to this forum, please help to undestand the the input.
Sorry i am not able to understand some of the basic terms. Please help me,
What is the input given below, can anyone suggest me to undestand it in a detailed,
Input:
The first line contains t, the number of test cases (about 1000). Then t test cases follow.
Each test case consists of a number N written in one line (1 <= N <= 1010).
I have a general question.
I have a general question. When we say palindromes in any base, would that mean all the numbers in that base are to be treated as single digits? To clarify, would AA in hexadecimal (176 in base 10) be considered a palindrome in base 16?
I have a general question.
I have a general question. When we say palindromes in any base, would that mean all the numbers in that base are to be treated as single digits? To clarify, would AA in hexadecimal (176 in base 10) be considered a palindrome in base 16?
@Kyun Yes. @Anilkumar Please
@Kyun Yes.
@Anilkumar Please go through our wiki at www.codechef.com/wiki. Please see solutions of problems which are open to all to understand what the input specifications mean.
what is the project format
what is the project format for submitting multiclass java or jar files
time out error ...... huh??
time out error ...... huh??
I am getting the same error
I am getting the same error "contest_problem_id field is required" time and again at the time of submission. May I know what the problem is?
Dear Admin, i am getting
Dear Admin,
i am getting TLE.
so my code is accepted ? only time limit exceeding ?
else my code also wrong.
dear admin, one more
dear admin,
one more doubt..
do i have to take all input at one time and give outputs for all?
else i 'l get one by one simultaneously?
Read the FAQ. TLE means time
Read the FAQ.
TLE means time limit exceeded. It doesn't not say anything at all about whether your code is correct or not.
Thank u stephen.. also tell
Thank u stephen..
also tell me your suggestion for my second quest.
I did. I told you to read the
I did. I told you to read the FAQ.
@sarath 4567 base 21 is 1080.
@sarath 4567 base 21 is 1080. 4567 base 25 is 787.
how to inclue problem_id.
how to inclue problem_id. some one help plz
is no one here? everytime i
is no one here? everytime i submit i am told about contest_problem_id field. how do i get rid of it?
@Rajat Log out and log in
@Rajat Log out and log in again.
continuing vaibhav shankar's
continuing vaibhav shankar's question, would one call 2424 to b a palindrome in base 100?
@k.venkata So you want to ask
@k.venkata
So you want to ask if your output is correct or not?
@deepankar no, i just want to
@deepankar
no, i just want to be sure with the definition of a palindrome in base b. i bliv 2424 is a pal. in base 100, and 4224 isnt, rt? im getting WA repeatedly, so just wanted to make sure that my definitions r rite....
how can we go to base more
how can we go to base more than 36?
we have 10 digits and 26 alphabet........so how can we go to base more than 36?
i hav tried to submit the
i hav tried to submit the solution to this problem many times now....but m gettin wrong answer...
so i was wondering if it is bcoz wen d input number is 0 or 1, the answer has to be 2.....right???
got it........sry
got it........sry
Hello Admin What should be
Hello Admin
What should be the value for the higest base here.???
Is it the number itself???
Please clarify...
For me the number 9991 is going beyond base 36 and still did not find any palindrome.
How should I represent a output for base more than 36. I have only this....
base[] = (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)
I have almost solved it.....just the few clarifiction will help me....before submitting....Plz reply
ok...I got the logic
ok...I got the logic
@ADMIN (CODECHEF) give us the
@ADMIN (CODECHEF)
give us the test case file too.
so that we will work on it.
my c-programme runs good in my pc and generating correct o/p for the test cases given.but, the codechef compiler saying "wrong answer"
would you pls give the test cases where my code goes wrong?
i made solution in java..When
i made solution in java..When i submit the system shows memory limit nearly 230,how shall i reduce it ??
note:I have imported some extra classes.
Is it not possible in java??
Hello Admin, I am getting
Hello Admin,
I am getting this error when I try to submit.
Site Offline
The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.
If you are the maintainer of this site, please check your database settings in the
settings.phpfile and ensure that your hosting provider's database server is running. For more help, see the handbook, or contact your hosting provider.It could be a random
It could be a random occurrence.
Thought I'd point this out to
Thought I'd point this out to people saying what if a number isn't a palindrome. All numbers are a palindrome in the base number-1. For example: 4678 is 11 (a palindrome) in base 4677
how to run this program in ur
how to run this program in ur compiler
#include <stdio.h>
int main (void)
{
int n;
int *p;
scanf("%d",&n);
p=new int[n];
return 0;
}
i m getting
/sources/tested.c: In function 'main': /sources/tested.c:6: error: 'use' undeclared (first use in this function) /sources/tested.c:6: error: (Each undeclared identifier is reported only once /sources/tested.c:6: error: for each function it appears in.) /sources/tested.c:6: error: expected ';' before 'namespace' /sources/tested.c:10: error: 'new' undeclared (first use in this function) /sources/tested.c:10: error: expected ';' before 'int'
type of error
what i want is to make and dynamic array
so how to achieve this..
Huge primes are painful. Can
Huge primes are painful. Can anyone refer a good number theory book/site?
@peeyush: I guess you would
@peeyush:
I guess you would need to use a malloc()/calloc()/realoc() call to assign memory dynamically in C.
When will the solutions be
When will the solutions
as the contest has ended can
as the contest has ended can anyone tell what was the logic behind the prob???i mean which property of the palindroms to b used here???
There is no particular
There is no particular properly of a palindrome. Just think of what it means to represent a number in a particular base. If you fix up the last digit for a number in a particular base, how would that affect the palindromic nature with the given constraints.
Folks might want to look up
Folks might want to look up the following URLs to get an idea of one approach to solving this problem.
http://en.wikipedia.org/wiki/Strictly_non-palindromic_number
http://www.mathpages.com/home/kmath359.htm
http://www.research.att.com/~njas/sequences/A016038
This approach would result in an O(n^0.5) algorithm.
I am new to Code Chef.. Where
I am new to Code Chef.. Where can I submit my solution
The problems have been moved
The problems have been moved to the practice section; you can submit this one at http://www.codechef.com/problems/K2/ (just get rid of DEC09 from the URL).
can anybody tell me what is
can anybody tell me what is wrong with this solution
I am getting all ans correct but it says wrong ans on codechef
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
string base_cal(long int num, long int base)
{
string str;
long int div;
int rem;
char ch;
while(num>1)
{
rem=num%base;
ch = (char)rem;
str.append(1,ch);
num=num/base;
if(num==1)
str.append(1,1);
}
return str;
}
int pallindrome(string s)
{
string str1;
int i;
for(i=s.length()-1;i>=0;i--)
{
str1.append(1,s.at(i));
}
if(str1==s)
return 1;
else
return 0;
}
int main()
{
int i,t;
long int num[1000], j;
cin>>t;
string s;
int check,val=2;
for(i=0;i<t;i++)
{
cin>>num[i];
if(num[i]==1)
cout<<val<<endl;
for(j=2;j<num[i];j++)
{
s = base_cal(num[i],j);
check = pallindrome(s);
if(check==1)
{
cout<<j<<endl;
break;
}
}
}
return 0;
}
#include<stdio.h>#include<ios
#include<stdio.h>
#include<iostream.h>
#include<string.h>
string base_cal(long int num, long int base)
{
string str;
long int div;
int rem;
char ch;
while(num>1)
{
rem=num%base;
ch = (char)rem;
str.append(1,ch);
num=num/base;
if(num==1)
str.append(1,1);
}
return str;
}
int pallindrome(string s)
{
string str1;
int i;
for(i=s.length()-1;i>=0;i--)
{
str1.append(1,s.at(i));
}
if(str1==s)
return 1;
else
return 0;
}
int main()
{
int i,t;
long int num[1000], j;
cin>>t;
string s;
int check,val=2;
for(i=0;i<t;i++)
{
cin>>num[i];
if(num[i]==1)
cout<<val<<endl;
for(j=2;j<num[i];j++)
{
s = base_cal(num[i],j);
check = pallindrome(s);
if(check==1)
{
cout<<j<<endl;
break;
}
}
}
return 0;
}
Hi Shivam, are you sure
Hi Shivam,
are you sure this code produce correct result..
i didnt think so..
I programmed it to success.
I programmed it to success. But i dont know how to upload can u plz help me?
sorry probably this might be
sorry probably this might be a closed one.. plz let me know if it is still open..
This was a contest problem.
This was a contest problem. The practice problem is at http://www.codechef.com/problems/K2/