/*by trial i found that the pattern that array follows is f[n]=2*f[n-1]+sumof[last 3 remainders]
ex:
f[i] =2 *f[i-1] + f[i-1]%f[i-2] + f[i-2]%f[i-3] + f[i-3]%f[i-4]
as i>7
or f[i] =2 *f[i-1]+add[i]
as add works for all values of i>2
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb=new StringBuffer("");
long[] add=new long[31];
add[3]=1;
add[4]=2;
add[5]=4;
for(int i=6;i<31;i++){
add[i]=add[i-3]+add[i-2]+add[i-1];
}
long []bg=new long[31];
bg[0]=0;
bg[1]=0;
bg[2]=1;
for(int i=3;i<31;i++){
bg[i]=2*bg[i-1]+add[i];
}
while(true){
int x=Integer.parseInt(br.readLine());
if(x==0)
break;
sb.append(bg[x-1]).append("\n");
}
System.out.println(sb);
}
}
ex:
f[i] =2 *f[i-1] + f[i-1]%f[i-2] + f[i-2]%f[i-3] + f[i-3]%f[i-4]
as i>7
or f[i] =2 *f[i-1]+add[i]
as add works for all values of i>2
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb=new StringBuffer("");
long[] add=new long[31];
add[3]=1;
add[4]=2;
add[5]=4;
for(int i=6;i<31;i++){
add[i]=add[i-3]+add[i-2]+add[i-1];
}
long []bg=new long[31];
bg[0]=0;
bg[1]=0;
bg[2]=1;
for(int i=3;i<31;i++){
bg[i]=2*bg[i-1]+add[i];
}
while(true){
int x=Integer.parseInt(br.readLine());
if(x==0)
break;
sb.append(bg[x-1]).append("\n");
}
System.out.println(sb);
}
}
No comments:
Post a Comment