Page 12 of 14
Re: 371"Ackermann Functions "
Posted: Mon Jan 18, 2010 5:36 pm
by mukit
@etameem:
this problem is straight-forward. just find and choose the best value.
The largest value in the sequence will not be larger than can be accomodated in a 32-bit Pascal LongInt or C long
use unsigned long or long long.
and ...
Code: Select all
max=0;
for (k=i;k<=j;k++) {
count=0;
p=k;
while (1)
{
if (p%2==0)
p=p/2;
else
p=3*p+1;
count=count+1;
if(p==1)break;
}
if(max<count) {
max=count;
gen=k;
}
count=0;
}
some additional...
1. Don't use count and max as variable name. sometimes it creates problem if defined as global.
2. Think simple

. It will take much time to understand your code.
3. It's better if you can find your bug yourself.
4. Always search the board first. Check with board input. If further confusion exists,
http://uvatoolkit.com/problemssolve.php
Sopno dekha uchit....
Posted: Fri Jun 25, 2010 12:23 pm
by shaon_cse_cu08
371 why TE? any body help me???help
Posted: Sat Jul 31, 2010 8:06 am
by @mjad
#include<stdio.h>
#include<stdlib.h>
//using namespace std;
//typedef __int64 ll;
int main()
{
long long s,e,n,c,t,p,m,temp,i;
while(scanf("%I64d %I64d",&s,&e)==2)
{
if(s==0&&e==0)
break;
t=0;
if(s>e)
{
temp=s;
s=e;
e=temp;
}
p=0;
for(i=s;i<=e;i++)
{
n=i;
c=0;
m=i;
do
{
if(n%2==0)
n/=2;
else
n=n*3+1;
c++;
}while(n!=1);
if(c>t){
t=c;
p=m;
}
}
printf("Between %I64d and %I64d, %I64d generates the longest sequence of %I64d values.\n",s,e,p,t);
}
return 0;
}
Re: 371 why TE? any body help me???help
Posted: Sat Jul 31, 2010 3:48 pm
by shaon_cse_cu08
Just use long long with %lld in ur input and output....u do not have to use field width then...
Don't Open a new thread... just search for the problem... There are many thread like this already in the board....
Try to send ur source code in between "
".... Thus it will be easier to read....

Re: 371 why TE? any body help me???help
Posted: Sun Aug 01, 2010 5:50 pm
by @mjad
thanks, for your reply
finally i got AC.

Re: 371"Ackermann Functions "
Posted: Sat Nov 27, 2010 8:26 pm
by dewsworld
Can anyone find me out the problem, please? I'm getting WA
Code: Select all
#include <stdio.h>
int main()
{
long long int a, b ;
long long int i ;
long long int maxSeq, maxSeqNum ;
long long int count, t ;
while( scanf("%lld%lld", &a, &b ) )
{
if( a+b == 0 ) break ;
maxSeq = 0 ;
for( i = a ; i <= b ; i++ )
{
t = i ;
count = 0 ;
do
{
if( t%2== 0 ) t = t/2 ;
else t = 3*t + 1 ;
++count ;
}while( t != 1 ) ;
if( count > maxSeq )
{
maxSeq = count ;
maxSeqNum = i ;
}
}
printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", a, b, maxSeqNum, maxSeq ) ;
}
return 0 ;
}
Re: 371"Ackermann Functions "
Posted: Sat Aug 20, 2011 1:05 am
by reja91
why i am getting WA???
#include<stdio.h>
int main()
{
unsigned long a,b,c,i,d,j,f,a1,b1,m,n;
while( scanf("%ld %ld", &a, &b)==2)
{
if(a==0 &&b==0)
{
break;
}
else
{
if(a>b)
{
a1=b;
b1=a;
}
else
{
a1=a;
b1=b;
}
f=0;
for(j=a1; j<=b1; j++)
{
i=j;
c=0;
while(1)
{
c++;
d++;
if(i==1)
{
break;
}
if(i%2!=0)
{
i= 3*i +1;
}
else
{
i= i/2;
}
}
if(c>f)
{
f=c;
m=j;
}
}
printf("Between %ld and %ld, %ld generates the longest sequence of %ld values.\n", a, b,m,f-1);
}
}
return 0;
}
Re: 371 - Anckerman Functions - Why WA ?
Posted: Sat Dec 31, 2011 7:28 am
by nebulousboy
can anyone help me telling why im getting wrong answer?????
# include <stdio.h>
int main()
{
long long n,i,j,m,k,l,number;
for(scanf("%lld %lld",&i,&j);i || j;scanf("%lld %lld",&i,&j))
{
m=0;
k=i;
for(k;k<=j;k++)
{
l=k;
for(n=1;;n++)
{
if(l%2!=0)
l=3*l+1;
else
l=l/2;
if(l==1)
break;
}
if(n>m)
{
m=n;
number=k;
}
}
printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n",i,j,number,m);
}
return 0;
}
Re: 371 - Anckerman Functions - Why WA ?
Posted: Fri Jan 06, 2012 11:12 pm
by brianfry713
nebulousboy, swap L and H if L>H.
Re: 371 - Anckerman Functions - Why WA ?
Posted: Tue Jan 17, 2012 12:46 pm
by jams_032
why im getting WA again and again.??
#include<stdio.h>
int main()
{ unsigned long i,n1,n2,n,c=1,p=0,temp,a,b,x;
while(scanf("%lu%lu",&n1,&n2)==2)
{ a=n1;
b=n2;
if(n1==0&&n2==0)
break;
else{
if(n1>n2)
{temp=n1;
n1=n2;
n2=temp;
}
p=0;
for(i=n1;i<=n2;i++)
{ c=0;
n=i;
while(n!=1)
{ if(n%2==0)
{ n=n/2;
}
else
{n=3*n+1;
}
c=c+1;
}
if(c>p)
{x=i;
p=c;
}
}
printf("Between %lu and %lu, %lu generates the longest sequence of %lu values.\n",a,b,x,p);
p=c;
}
}
return 0;
}
Re: 371 - Anckerman Functions - Why WA ?
Posted: Thu Jan 19, 2012 1:16 am
by brianfry713
For input "1 2" the output should be:
Between 1 and 2, 1 generates the longest sequence of 3 values.
Your output is:
Between 1 and 2, 2 generates the longest sequence of 1 values.
Re: 371 - Anckerman Functions - Why WA ?
Posted: Sun Jan 29, 2012 12:26 pm
by bristy1588
Can anyone please help me?? I am getting WA, but i dont know for which data.
Here is my codeL
Code: Select all
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
long long dp[5000005];
int main()
{
int a,b,l,h,i,num;
long long ans,ct;
long long n;
dp[1]=0;
while(1)
{
scanf("%d %d",&a,&b);
if((a==0) &&(b==0))
break;
l=min(a,b);
h=max(a,b);
ans=0;
for(i=l;i<=h;i++)
{
n=i;
ct=0;
while(1)
{
if(n==1)
{
if(i<5000005)
dp[i]=ct;
if(ct>ans)
{
ans=ct;
num=i;
}
break;
}
if(n<5000005)
{
if(dp[n]!=0)
{
ct=ct+dp[n];
if(i<5000005)
dp[i]=ct;
if(ct>ans)
{
ans=ct;
num=i;
}
break;
}
}
if((n%2)==0)
n=n/2;
else
n=3*n+1;
ct++;
}
}
printf("Between %d and %d, %d generates the longest sequence of %lld values.\n",l,h,num,ans);
}
return 0;
}
Re: 371 - Anckerman Functions - Why WA ?
Posted: Mon Jan 30, 2012 10:20 pm
by sohel
You can test with your own input in this site
http://uvatoolkit.com/problemssolve.php.
Re: 371 - Anckerman Functions
Posted: Sat May 19, 2012 9:56 pm
by kia.masster
Hello. I was wondering if you might be able to help me! Why I got RE for this problem? Here is my code:
Thank You
371 - Ackermann Functions... Why WA????
Posted: Sat May 19, 2012 11:45 pm
by jocker1
Code: Select all
#include <iostream>
#include <algorithm>
using namespace std;
unsigned long long calculate(unsigned long long n);
int main(){
unsigned long long a,b,d,count,c;
while(cin>>a>>b){
if(a==0 && b==0) break;
if(a>b)
swap(a,b);
d=0;
c=0;
for(long long i=a;i<=b;i++)
{
count = calculate(i);
if(d<count)
{d=count;
c=i;}
}
cout<<"Between "<<a<<" and "<<b<<", "<<c<<" generates the longest sequence of "<<d<<" values."<<endl;
}
return 0;
}
unsigned long long calculate(unsigned long long n){
long long cer=0;
while(n!=1){
if(n%2==0) n=n/2;
else n=(3*n)+1;
cer++;
}
return cer;
}