首页 > 专题系列 > Java解POJ > POJ 3734 Blocks [解题报告] Java
2013
11-13

POJ 3734 Blocks [解题报告] Java

Blocks

问题描述 :

Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.

输入:

The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.

输出:

For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.

样例输入:

2
1
2

样例输出:

2
6

解题代码:

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {

/**
 * @param args
 * @return 
 */
 static int pow_mod(int a,int b,int c)
 {
	int r=1,d=a;
	while(b>0){
		if((b&1)==1) r = r*d%c;
		d = d*d%c;
		b>>=1;
	}
	return r;
  }
  
public static void main(String[] args)throws Exception {
	// TODO Auto-generated method stub
	int ans,t,n;
	Scanner cin = new Scanner(System.in);
	
	t = cin.nextInt();
	for(int i=0;i< t;i++)
	{
		n = cin.nextInt();
		ans = pow_mod(2,n-1,10007);
		ans = (ans+1) *ans %10007;
		System.out.println(ans);
	}
  }

}

  1. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”

  2. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;