1. 程式人生 > >HOJ 6433 Problem H. Pow

HOJ 6433 Problem H. Pow

Problem H. Pow

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1521    Accepted Submission(s): 721

 

Problem Description

There are n numbers 3^0, 3^1, . . . , 3^n-1. Each time you can choose a subset of them (may be empty), and then add them up.
Count how many numbers can be represented in this way.

 

Input

The first line of the input contains an integer T , denoting the number of test cases. In each test case, there is a single integers n in one line, as described above.
1 ≤ T ≤ 20, 0 ≤ n ≤ 1000

 

Output

For each test case, output one line contains a single integer, denoting the answer.

 

Sample Input

4

9

7

8

233

 

Sample Output

512

128

256

13803492693581127574869511724554050904902217944340773110325048447598592

 
 

 

知識點:string

 

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

string str[1010];

void init()
{
	str[0] = "1";
	bool flag = false;
	for(int i=1; i<=1000; i++) {
		string s = "";
		for(int j=str[i-1].size()-1; j>=0; j--) {
			int t = (str[i-1][j]-'0')*2;
			if(flag) {
				t++;
				flag = false;
			}
			if(t<10) {
				s = (char)(t+'0') + s;
			}else {
				s = (char)(t-10+'0') + s;
				flag = true;
			}
		}
		if(flag) s = "1" + s;
		str[i] = s;
		flag = false;
	}
}

int main()
{
	int T_T;
	init();
	cin >> T_T;
	while(T_T--) {
		int n;
		cin >> n;
		cout << str[n] <<endl;
	}
	return 0;
}