I don't know why my solution received TLE. It works well my VC++ and
g++ compiler.
Anyone tell me why my solution received TLE. Thx.
Code: Select all
Remove my accepted code
Moderator: Board moderators
Code: Select all
Remove my accepted code
Code: Select all
remove after soyoja's AC.
Code: Select all
#include <iostream.h>
int fact(int n)
{int p;
p=1;
if(n==0)return 1;
else{
while(n!=0)
{p*=n;n--;}
return p;}}
int max(int i,int j)
{if(i>j)return i;
else return j;
}
int combination(int j,int n)
{int p,k;
p=1;
if(n==j) return 1;
else{
k=n;
while(k!=j)
{p*=k;k--;}
return p/fact(n-j);}
}
int main(void)
{int n;
int unsigned long s;
while(cin>>n){
if(n==0)return 0;
else{
s=0;
for(int i=0;i<=n/2;i++)
for(int j=0;j<=n;j++)
if(2*i+j==n){s+=combination(max(i,j),i+j);}
cout<<s<<endl;}}
return 0;}
Code: Select all
39
40
0
Code: Select all
102334155
165580141
could anyone explain me, why this work ?rio wrote:Code: Select all
remove after soyoja's AC.
Code: Select all
ACCEPTED
Code: Select all
public static void main(String[] args)
{
while (true)
{
// something to read the number and stock it in the variable "N"
if(N == 0)break;
System.out.println(solve(N));
}
}
static long solve(int N)
{
if(N == 1)return 1;
if(N == 2)return 2;
if(N == 3)return 3;
long[] fib = new long[N];
fib[0] = 1; fib[1] = 2; fib[2] = 3;
for(short i = 3; i < N; i++) fib[i] = fib[i-1] + fib[i-2];
return fib[N-1];
}
}
Code: Select all
import java.io.*;
import java.util.*;
class teste2
{
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
// String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main(String args[])
{
while (true)
{
String input = ReadLn (255);
StringTokenizer st = new StringTokenizer(input);
int N = Integer.parseInt(st.nextToken());
if(N == 0)break;
System.out.println(solve(N));
}
}
static long solve(int N)
{
if(N == 1)return 1;
if(N == 2)return 2;
if(N == 3)return 3;
int[] fib = new int[N];
fib[0] = 1; fib[1] = 2; fib[2] = 3;
for(short i = 3; i < N; i++) fib[i] = fib[i-1] + fib[i-2];
return fib[N-1];
}
}