杭电acm-------2502月之数

来源:互联网 发布:七天网络网页版 编辑:程序博客网 时间:2024/05/29 10:22

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

解题思路:n二进制最前面的数全是1,共有2^(n-1)个排列,所有前面的1的个数有2^(n-1)。其后的位数无非就是0和1,而0和1各占一半,所有后面1所占的个数为(2^(n-1))*(n-1)/2。因而n二进制对应的月之数为其后1的个数加上前面1的个数,为(2^(n-1))*(n-1)/2+2^(n-1)。

#include <iostream>using namespace std;int main(){int n,t,s,b,a,i;while(cin>>n){while(n--){cin>>t;s=1;for(i=1;i<t;i++){s*=2;}a=((t-1)*s)/2;b=s+a;cout<<b<<endl;}}return 0;}

0 0
原创粉丝点击