Problem:
Problem A: Ohgas' Fortune

The Ohgas are a prestigious family based on Hachioji. The head of the family,
Mr. Nemochi Ohga, a famous wealthy man, wishes to increase his fortune
by depositing his money to an operation company. You are asked to help
Mr. Ohga maximize his profit by operating the given money during a specified
period.

From a given list of possible operations,
you choose an operation to deposit the given fund to.
You commit on the single operation throughout the period and
deposit all the fund to it.

Each operation specifies an annual interest rate, whether the interest
is simple or compound, and an annual operation charge.

An annual operation charge is a constant
not depending on the balance of the fund.

The amount of interest is calculated at the end of every year, by
multiplying the balance of the fund under operation by the annual
interest rate, and then rounding off its fractional part. For compound
interest, it is added to the balance of the fund under operation, and
thus becomes a subject of interest for the following years. For
simple interest, on the other hand, it is saved somewhere else and
does not enter the balance of the fund under operation (i.e. it is
not a subject of interest in the following years).

An operation charge is then subtracted from the balance of the fund
under operation. You may assume here that you can always pay the
operation charge (i.e. the balance of the fund under operation is
never less than the operation charge).

The amount of money you obtain after the specified years of operation
is called ``the final amount of fund.'' For simple interest, it is
the sum of the balance of the fund under operation at the end of the
final year, plus the amount of interest accumulated throughout the
period. For compound interest, it is simply the balance of the fund
under operation at the end of the final year.

Operation companies use C, C++, Java, etc., to perform their
calculations, so they pay a special attention to their interest
rates. That is, in these companies, an interest rate is always an
integral multiple of 0.0001220703125 and between 0.0001220703125 and
0.125 (inclusive). 0.0001220703125 is a decimal representation of
1/8192. Thus, interest rates' being its multiples means that they can
be represented with no errors under the double-precision binary
representation of floating-point numbers.

For example, if you operate 1000000 JPY for five years with an annual,
compound interest rate of 0.03125 (3.125 %) and an annual operation charge of 3000 JPY, the balance changes as
follows.

The balance of the fund under operation
(at the beginning of year)

Interest

The balance of the fund under operation (at the end of year)

A

B = A × 0.03125 (and rounding off fractions)

A + B - 3000

1000000

31250

1028250

1028250

32132

1057382

1057382

33043

1087425

1087425

33982

1118407

1118407

34950

1150357

After the five years of operation, the final amount of fund is 1150357 JPY.

If the interest is simple with all other parameters being equal,
it looks like:

The balance of the fund under operation (at the beginning of year)

Interest

The balance of the fund under operation (at the end of year)

Cumulative interest

A

B = A × 0.03125 (and rounding off fractions)

A - 3000

1000000

31250

997000

31250

997000

31156

994000

62406

994000

31062

991000

93468

991000

30968

988000

124436

988000

30875

985000

155311

In this case the final amount of fund is the total of the fund under
operation, 985000 JPY, and the cumulative interests, 155311 JPY, which
is 1140311 JPY.

Input

The input consists of datasets. The entire input looks like:

the number of datasets (=m)

1st dataset

2nd dataset

...

m-th dataset

The number of datasets, m, is no more than 100.
Each dataset is formatted as follows.

the initial amount of the fund for operation

the number of years of operation

the number of available operations (=n)

operation 1

operation 2

...

operation n

The initial amount of the fund for operation, the number of years of operation,
and the number of available operations are all positive integers.
The first is no more than 100000000, the second no more than 10, and
the third no more than 100.

Each ``operation'' is formatted as follows.

simple-or-compound annual-interest-rate annual-operation-charge

where simple-or-compound is a single character of either '0' or '1',
with '0' indicating
simple interest and '1' compound. annual-interest-rate is represented
by a decimal fraction and is an integral multiple of 1/8192.
annual-operation-charge is an integer not exceeding 100000.

Output

For each dataset, print a line having a decimal integer indicating the
final amount of fund for the best operation. The best operation is the
one that yields the maximum final amount among the available
operations. Each line should not have any character other than
this number.

You may assume the final balance never exceeds 1000000000.
You may also assume that at least one operation has the final amount of the
fund no less than the initial amount of the fund.

Sample Input

4
1000000
5
2
0 0.03125 3000
1 0.03125 3000
6620000
7
2
0 0.0732421875 42307
1 0.0740966796875 40942
39677000
4
4
0 0.0709228515625 30754
1 0.00634765625 26165
0 0.03662109375 79468
0 0.0679931640625 10932
10585000
6
4
1 0.0054931640625 59759
1 0.12353515625 56464
0 0.0496826171875 98193
0 0.0887451171875 78966

Output for the Sample Input

1150357
10559683
50796918
20829397

Solution in JAVA (judge verdict: Accepted)

import java.util.*;
import java.util.regex.*;

import static java.lang.Math.*;
import static java.lang.System.out;

// AOJ
public class Main {
	final Scanner sc=new Scanner(System.in);
	public static void main(String[] args) {
		new Main().AOJ1135();
	}
	
	void AOJ1136(){
		while(sc.hasNext()){
			int n=sc.nextInt();
			if(n==0)	break;
			solve1136(n);
			out.println("+++++");
		}
	}
	void solve1136(int N){	// N本の折れ線
		// 元の直線0
		int a0=sc.nextInt();
		int[] v0=new int[a0], len0=new int[a0];
		int lx=sc.nextInt(), ly=sc.nextInt();
		for(int i=1; i<a0; i++){
			int x=sc.nextInt(), y=sc.nextInt();
			v0[i]=v1136(x,y,lx,ly);
			len0[i]=max(abs(y-ly),abs(x-lx));
			lx=x;	ly=y;
		}
		for(int i=1; i<=N; i++){
			int a=sc.nextInt();
			if(a!=a0)	continue;
			int[] v=new int[a], len=new int[a];
			lx=sc.nextInt();	ly=sc.nextInt();
			for(int j=1; j<a; j++){
				int x=sc.nextInt(),y=sc.nextInt();
				v[j]=v1136(x,y,lx,ly);
				len[j]=max(abs(y-ly),abs(x-lx));
				lx=x;	ly=y;
			}
			if(isSame(v0,len0,v,len))	out.println(i);
			else if(isSame2(v0,len0,v,len))	out.println(i);
		}
	}
	int v1136(int x, int y, int lx, int ly){
		if(y>ly)	return 1;
		else if(x>lx)	return 2;
		else if(y<ly)	return 3;
		else if(x<lx)	return 4;
		return 0;
	}
	boolean isSame(int[] v0, int[] len0, int[] v, int[] len){
		for(int i=1; i<len.length; i++){
			if(len0[i]!=len[i])	return false;
		}
		int ans=0;
		for(int j=0; j<4; j++){
			for(int i=1; i<v.length; i++){
				if(v0[i]!=v[i]){
					v=turn1136(v,1);
					ans++;
					break;
				}
			}
			if(ans==j)	break;
		}
		return ans!=4;
	}
	boolean isSame2(int[] v0, int[] len0, int[] v, int[] len){
		for(int i=1; i<v.length; i++){
			if(len0[i]!=len[v.length-i])	return false;
		}
		int ans=0;
		for(int j=0; j<4; j++){
			for(int i=1; i<v.length; i++){
				if(v0[i]!=v[v.length-i]){
					v=turn1136(v,1);
					ans++;
					break;
				}
			}
			if(ans==j)	break;
		}
		return ans!=4;
	}
	int[] turn1136(int[] v, int n){
		if(n==0)	return v;
		int[] ans=new int[v.length];
		for(int i=1; i<v.length; i++){
			ans[i]=v[i]+n>4? 1: v[i]+n;
		}
		return ans;
	}
	
	void AOJ1135(){
		int N=sc.nextInt();
		for(int I=1; I<=N; I++){
			int a=sc.nextInt(), year=sc.nextInt(), n=sc.nextInt(), ans=0;
			for(int i=0; i<n; i++){
				if(sc.nextInt()==1)	ans=max(ans, huku(a,year,sc.nextDouble(),sc.nextInt()));
				else	ans=max(ans, tan(a,year,sc.nextDouble(), sc.nextInt()));
			}
			out.println(ans);
		}
	}
	int huku(int a, int year, double rate, int t){
		for(int i=0; i<year; i++){
			int temp=(int) (a*rate);
			a+=temp-t;
		}
		return a;
	}
	int tan(int a, int year, double rate, int t){
		int r=0;
		for(int i=0; i<year; i++){
			r+=a*rate;
			a-=t;
		}
		return a+r;
	}
	
	void AOJ1142(){
		int N=sc.nextInt();
		for(int I=1; I<=N; I++){
			HashMap<String,Integer> map=new HashMap<String,Integer>();
			String s=sc.next();
			for(int i=1; i<s.length(); i++){
				String a=s.substring(0, i), b=s.substring(i);
				String aa=new StringBuilder(a).reverse().toString(), bb=new StringBuilder(b).reverse().toString();
				//out.println("A:"+a+" AA:"+aa+" B:"+b+" BB:"+bb);
				map.put(a+b,1);
				map.put(aa+b,1);
				map.put(a+bb,1);
				map.put(aa+bb,1);
				map.put(b+a,1);
				map.put(bb+a, 1);
				map.put(b+aa, 1);
				map.put(bb+aa, 1);
				//out.println(map);
			}
			
			out.println(map.size());
		}
	}
	
	void AOJ1137(){
		int N=sc.nextInt();
		for(int I=1; I<=N; I++){
			int a=toInt(sc.next()),b=toInt(sc.next());
			//out.println("A:"+a+" B:"+b);
			out.println(toMcxi(a+b));
		}
	}
	int toInt(String s){
		int ans=0,temp=1;
		char[] c=s.toCharArray();
		for(int i=0; i<c.length; i++){
			if(c[i]=='m'){
				ans+=temp*1000;
				temp=1;
			}else if(c[i]=='c'){
				ans+=temp*100;
				temp=1;
			}else if(c[i]=='x'){
				ans+=temp*10;
				temp=1;
			}else if(c[i]=='i'){
				ans+=temp;
				temp=1;
			}else{
				temp=Character.digit(c[i],10);
			}
		}
		return ans;
	}
	String toMcxi(int n){
		StringBuilder sb=new StringBuilder();
		if(n/1000>0){
			sb.append(n/1000==1?"m":(n/1000)+"m");
			n%=1000;
		}
		if(n/100>0){
			sb.append(n/100==1?"c":(n/100)+"c");
			n%=100;
		}
		if(n/10>0){
			sb.append(n/10==1?"x":(n/10)+"x");
			n%=10;
		}
		if(n>0)	sb.append(n==1?"i":n+"i");
		return sb.toString();
	}
	
	void AOJ0142(){
		
	}
	
	void AOJ0145(){
		int n=sc.nextInt();
		
	}
	
	void AOJ0141(){
		int N=sc.nextInt();
		for(int I=1; I<=N; I++){
			int n=sc.nextInt();
			char[][] c=new char[n+4][n+4];
			int[] vx={0, 1, 0, -1};
			int[] vy={-1, 0, 1, 0};
			c=solve141(2, n+1, n, c, vx, vy);
			for(int j=2; j<=n+1; j++){
				for(int i=2; i<=n+1; i++)	out.print(c[i][j]);
				out.println();
			}
			out.println();
		}
	}
	char[][] solve141(int x, int y, int n, char[][] c, int[] vx, int[] vy){
		//範囲を超えていないか
		if(x<2&&x>n+1 && y<2&&y>n+1)	return c;
		//周りが#
		//return c;
		
		c[x][y]='#';
		
		// 1周目
		if(x==2){
			if(y!=2)	solve141(x, y-1, n, c, vx, vy);
			else	solve141(x+1, y, n, c, vx, vy);
		}
		if(y==2){
			if(x!=n+1)	solve141(x+1, y, n, c, vx, vy);
			else	solve141(x, y+1, n, c, vx, vy);
		}
		if(x==n+1){
			if(y!=2)	solve141(x, y+1,n,c,vx,vy);
			else	solve141(x-1, y,n,c,vx,vy);
		}
		if(y==n+1){
			if(x!=3)	solve141(x-1,y,n,c,vx,vy);
			else	solve141(x,y-1,n,c,vx,vy);
		}
		
		// それ以降
		for(int i=0; i<4; i++){
			if(c[x+vx[i]][y+vy[i]]!='#'){
				solve141(x+vx[i], y+vy[i],n,c,vx,vy);
			}
		}

		return c;
	}
	
	
	void AOJ0139(){
		int N=sc.nextInt();
		final Pattern ap=Pattern.compile("^>'(=+)#(=+)~$");
		for(int i=1; i<=N; i++){
			String s=sc.next();
			Matcher m=ap.matcher(s);
			if(m.matches()){
				if(m.group(1).length()==m.group(2).length()){
					out.println("A");
					continue;
				}
			}else if(Pattern.compile("^>\\^(Q=)+~~$").matcher(s).matches()){
				out.println("B");
				continue;
			}
			out.println("NA");
		}
	}
	
	void AOJ0137(){
		int N=sc.nextInt();
		for(int i=1; i<=N; i++){
			long s=sc.nextLong();
			out.println("Case "+i+":");
			for(int j=0; j<10; j++){
				s*=s;	s/=100;	s%=10000;
				out.println(s);
			}
		}
	}
	
	ArrayList<Integer> Sieve2(int N){
	    ArrayList<Integer> prime = new ArrayList<Integer>();
	    boolean[] list = new boolean[N+1];
	    Arrays.fill(list, true);
	    list[1]=false;
	    for (int i=2; i<=N; i++) {
	        if(list[i]) {
	            prime.add(i);
	            for (int j=i+i; j<=N; j+=i)    list[j] = false;
	        }
	    }
	    return prime;
	}

	boolean[] Sieve(int N){
	    boolean[] list = new boolean[N+1];
	    Arrays.fill(list, true);
	    list[1]=false;
	    for(int i=2; i<=N; i++) {
	        if(list[i]) {
	            for (int j=i+i; j<=N; j+=i)     list[j] = false;
	        }
	    }
	    return list;
	}
	
	boolean isPrime(int n){
	    for(int i=2; i*i<=n; i++){
	        if(n%i==0)     return false;
	    }
	    return true;
	}
	
	int gcd(int x, int y){
		if(y==0)	return x;
		else	return gcd(y, x%y);
	}
}