码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2502 [月之数] 分析

时间:2017-02-05 10:44:36      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:--   pow   std   clu   math   cin   int   cpp   1出现的次数   

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

题目大意:n位的二进制数称为n二进制数。求所有n二进制数共有几个1。

关键思想:要每一位考虑1出现的次数。看个例子就很清楚了。

4二进制数

1 0 0 0

1 0 0 1

1 0 1 0

1 0 1 1

1 1 0 0

1 1 0 1

1 1 1 0

1 1 1 1    注意到首位有2的(4-1)次方个1,之后每一位因为都有分与合,每一位都有2的(4-2)次方个,所以f(n)=2^(n-1)+(n-1)*2^(n-2).

//看各位可以有几次1
 
#include <iostream>
#include <math.h>
using namespace std;

int main(){
	int T,N;
	int a[22];
	cin>>T;
	for(int i=1;i<=21;i++)
		a[i]=pow(2,i-1)+(i-1)*pow(2,i-2);
	while(T--){
		cin>>N;
		cout<<a[N]<<endl;
	}
	return 0;
} 

  

HDU 2502 [月之数] 分析

标签:--   pow   std   clu   math   cin   int   cpp   1出现的次数   

原文地址:http://www.cnblogs.com/G-M-WuJieMatrix/p/6366753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!