TOJ 2920 – Help Hakim

Solution of http://acm.tju.edu.cn/toj/showp2920.html

Using dynamic programming :

import java.util.*;
class Main{
	public static void main(String args[]){
		Scanner in = new Scanner(System.in);
		long simpan[] = new long[1000001];
		long hasil[] = new long[1000001];
		int hitung = 4;
		
		while(in.hasNext()){
			int n = in.nextInt();
			
			simpan[0] = 0;
			simpan[1] = 1;
			simpan[2] = 3;
			simpan[3] = 6;
			
			hasil[0] = 0;
			hasil[1] = 1;
			hasil[2] = 3;
			hasil[3] = 10;
			while(hitung<=n){
				simpan[hitung]+=3*(hitung-1)+simpan[hitung-3];
				hasil[hitung]=hasil[hitung-1]+simpan[hitung];
				hitung++;
			}
			
			System.out.println(hasil[n]);
		}
	}
}

TOJ 1726 ‘World Cup Noise’

The problem wants you to determine the number of n-bit sequences which contain no adjacent ones. For example, the sequence 10100 doesn’t contain any adjacent ones, but the sequence 101101110 does.

EDT: Solution

When n = 0, there is one possibility: the empty sequence.

When n = 1, there are two possibilities: 0, 1.

When n = 2, there are three possibilities: 00, 01, 10. 11 contains adjacent ones, so it is excluded from the set. This can be verified by hand.

When n = 3, there are five possibilities: 000, 001, 010, 100, 101.

When n = 4, there are eight possibilities: 0000, 0001, 0010, 0100, 0101, 1000, 1001, 1010.

When n = 5, there are thirteen possibilities: 00000, 00001, 00010, 00100, 00101, 01000, 01001, 01010, 10000, 10001, 10010, 10100, 10101.

1, 2, 3, 5, 8, 13, and so on; doesn’t this seem familiar? It happens to be some sort of Fibonacci sequence, but how?

How do we form a sequence of length n+1 from shorter sequences so that all sequences do not have any adjacent ones?

We can append 0 to any sequence which ends with 0, or append 1 to a sequence which ends with 0, or append 0 to a sequence which ends with 1. All sequences do not have adjacent ones.

Let x n be the number of n-bit sequences which do not contain adjacent ones.

How many n-bit sequences end with 0 and do not have adjacent ones?

e(n,0) = x(n-1)

How many n-bit sequences end with 1 and do not have adjacent ones?

e(n,1) = x(n) - e(n,0)
e(n,0) + e(n,1) = x(n)
x(n+1) = e(n,0) + e(n,0) + e(n,1)
x(n+1) = e(n,0) + x(n)
x(n+1) = x(n-1) + x(n)
x(n) = x(n-1) + x(n-2)
x(0) := 1
x(1) := 2
x(n) = 1,2,3,5,8,13,21,34,55,89 ... (n from 0)

Indeed it is a sequence which has the same relation as the Fibonacci sequence, but different initial conditions.

Since the input space is small (input is limited so that it is always less than 45), we can precompute the answers first to generate a table of answers.

Angloid.

L = array 1 2
for i from 2 to 45 exclusive
   L[i] := L[i-1] + L[i-2]
read integer n
for i from 1 to n
   write-line 'Scenario #' i ':'
   write-line L[i]
   write-line

C.

#include <stdio.h>
#define N 45
int main () {
     int i;
     int n;
     int j;
     int L[N];
     L[0] = 1;
     L[1] = 2;
     for (i = 2; i < N; ++i) {
          L[i] = L[i-1] + L[i-2];
     }
     scanf ("%d", &n);
     for (i=1; i <= n; ++i) {
          scanf ("%d", &j);
          printf ("Scenario #%d:\n%d\n\n", i, L[j]);
     }
     return 0;
}

FD: Solution

Find the recurrence relation which describes the number of the two or more consecutive ones in n-bits. Since 0 and 1-bit can’t have two or more consecutive ones, we set array[0] and array[1] to 0.
The recurrence relation is:

f(n) = f(n-1) + f(n-2) + pow(2,n-2)

The number of n-bit sequences which contain no adjacent ones is pow(2,n) - f(n).

This problem isn’t solveable with recursion; you’ll get TLE (Time Limit Exceeded). So, we use Dynamic programming here to save the value we have counted before.

This is the solution with Java.

import java.util.*;

class Main{
	public static void main(String args[]){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int i=1;
		long rek[] = new long[46];
		rek[0] = rek[1] = 0;
		int batas = 2;
		while(n-->0){
			int m = in.nextInt();
			for(int ii=batas ; ii<=m ; ii++){
				rek[ii] = rek[ii-2]+rek[ii-1]+(long)Math.pow(2,ii-2);	
			}
			System.out.println("Scenario #"+i+":");
			System.out.println((long)Math.pow(2,m)-rek[m]);
			i++;
			batas = m+1;
		}
	}
}

TOJ 1676 ‘Networking’

C++.

#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
class DisjointSets{
	public:
		DisjointSets(int si){
			size = si;
			s = new int[size];
			for(int i=0 ; i<size; i++)
				s[i] = -1;
		}
		void gabung(int a,int b){
			if(s[a]<s[b])
				s[a] = b;
			else{
				if(s[a]==s[b])
					s[a]--;
				s[b] = a;
			}
		}
		int find(int x){
			if(s[x]<0)
				return x;
			else
				return s[x] = find(s[x]);
		}
		int size;
		int* s;
};
class Edge{
	public:
		Edge(int s,int d,int w){
			source = s;
			dest = d;
			weight = w;
		}
		bool operator<(const Edge& e) const{
			return weight<e.weight;
		}
		int source;
		int dest;
		int weight;
};
int main(){
	int n;
	cin>>n;
	while(n!=0){
		int route;
		cin>>route;
		DisjointSets d = DisjointSets(route);
		vector<Edge> list;
		for(int i=0 ; i<route ; i++){
			int s,d,w;
			cin>>s>>d>>w;
			list.push_back(Edge(s-1,d-1,w));
		}
		sort(list.begin(),list.end());
		int hasil=0;
		for(int i=0 ; i<route ; i++){
			Edge temp = list[i];
			int a = d.find(temp.source);
			int b = d.find(temp.dest);
			if(a!=b){
				hasil+=temp.weight;
				d.gabung(a,b);
			}
		}
		cout<<hasil<<endl;
		cin>>n;
	}
}

TOJ 1001 ‘Hello, World!’

The C standard library happens to have all the nuts and bolts we need.

An ASCII character is 7 bits wide, but is stored as a byte (a 8-bit quantity) and its most significant bit is set to zero. There are 128 ASCII characters (the seventh power of 2 is 128).

Read a string; interpret it as an integer; then write the integer as a character.

Angloid.

until eof
   read token Integer i
   write Byte i

C.

#include <stdio.h>

int main () {
     int i;
     while (scanf ("%d", &i) == 1) {
          printf ("%c", i);
     }
     return 0;
}

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Design a site like this with WordPress.com
Get started