hdu 2502月之数

来源:互联网 发布:阿里云 备案 编辑:程序博客网 时间:2024/05/11 00:58

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2502


分析:
比如n=4时,有:
1000
1001
1010
1011
1100
1101
1110
1111
可以看到。除了第一位剩下的有
000
001
010
011
100
101
110
111
可以数一下,0和1的各为二分之一。于是算一下0和1总个数,除以2就好了。
即为:(2^(n-1)*(n-1))/2;

   
再加上第一位1总个数:2^(n-1) 得月之数 
即ans= 2^(n-2)*(n-1)  + 2^(n-1)

代码如下:

#include<cstdio>#include<cmath>int main(){int t,n,s;while(~scanf("%d",&t)){while(t--){s = 0;scanf("%d",&n);s = pow(2.0,n-1)+(pow(2.0,n-1)*(n-1))/2;printf("%d\n",s);}}return 0;}


1 0