牛课 I.HPU's birthday(思维)

题目链接:点击这里
在这里插入图片描述
在这里插入图片描述
思路:统计 " 110 " "110" 的个数,则统计以每一个 " 0 " "0" 结束时,看他前面能组成多少个 " 11 " "11" (即 C n u m 2 C_{num}^{2}

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 1010;

int t, tmp, n;
int a[maxn];

int main()
{
	scanf("%d", &t);
	while(t--)
	{
		memset(a, 0, sizeof(a));
		scanf("%d", &n);
		tmp = n;
		int cnt = 0;
		while(tmp)
		{
			a[++cnt] = tmp % 2;
			tmp /= 2;
		}
		
		ll num = 0;
		ll ans = 0;
		for(int i = 1; i <= n; i++)
		{
			for(int j = cnt; j >= 1; j--)
			{
				if(a[j])	num++;
				else	ans = (ans + num*(num-1)/2) % MOD;
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}
发布了673 篇原创文章 · 获赞 103 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104055923