题目链接:http://www.spoj.com/problems/GOO/
题目大意:求n个二进制为能够组成多少个数以及组成这些数需要多少个1. 例如3位可以组成 100 101 110 111 这四个数,共需要8个数字。
解题思路:很简单的一道题,但是一定要注意溢出!!! 1 << x 如果超出整型范围是会溢出的,一种解决方法是先将1赋值给一个long long型变量,然后进行移位操作。
代码:
1 const int maxn = 1e4 + 5; 2 ll p[100]; 3 int n, k; 4 5 void dowork(){ 6 memset(p, 0, sizeof(p)); 7 for(int i = 1; i <= 55; i++){ 8 p[i] = 1; 9 p[i] <<= (i - 1); 10 for(int j = 1; j < i; j++) p[i] += p[j]; 11 } 12 } 13 void solve(){ 14 ll ans1 = 1, ans2 = p[n]; 15 ans1 <<= (n - 1); 16 printf("%lld %lld ", ans1, ans2); 17 } 18 19 int main(){ 20 dowork(); 21 int t; 22 scanf("%d", &t); 23 while(t--){ 24 scanf("%d", &n); 25 solve(); 26 } 27 }
题目:
GOO - Game Of Ones
Haba and Goba are brothers, they love binary numbers very much. Today is Haba’s birthday so, his uncle gifted him a 3 bit binary string. Getting the present, both of them are very happy. Now, Goba asked Haba that using 3 bits how many different binary numbers he can write, whose 1st bit is 1 and total how many 1 will be needed to write those numbers. Haba answered he can write 4 different numbers using 3 bits whose first bit is 1 and those numbers are 4(100), 5(101), 6(110) and 7(111) and to write these 4 numbers in binary he will need 8 ‘1’. Now Goba told Haba that he will tell Haba the length of a binary string n and Haba will have to answer that using n bits how many different binary numbers he can write, whose 1st bit is 1 and total how many 1 will be needed to write those numbers. If Haba can answer correctly he will get more binary strings as present. Haba wants to get more binary string as present but, he doesn’t know the answer of the question of Goba for bigger value of n, so now he wants your help. Help him to find the answer.
Input
The first line contains a positive integer number, t (1≤t≤50) indicating the number of test cases. Each test case contains a positive integer number, n (1≤n≤50) indicating the number of bits.
Output
For each test case you have to output two numbers, whose first and second number indicates the answer of Goba’s first and second question respectively.
Example
Input:
2
2
3
Output:
2 3
4 8