列表

详情


NC200601. HPU's birthday

描述

2019.10.20 is the 110th birthday of HPU. On that day, you can see the string "110" everywhere.
Hery is given an integer n in decimal, he can transform n to a **binary string** S. For example: , so 6 can be transformed to "110". But hery thinks S is too short, so he constructs n's **birthday string** T by repeating n's **binary string** S for n times. So for n=6, n's **birthday string** T is:

"110110110110110110"

For n's **birthday string** T, hery wants you to calculate how many pairs (i,j,k) satisfy and .

输入描述

The first line is an integer t, the number of test cases.
Next t lines, for each line, there is an integer n.

输出描述

For each n, output the answer mod .

示例1

输入:

4
1
2
3
11

输出:

0
1
0
1815

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 15ms, 内存消耗: 500K, 提交时间: 2020-01-27 20:06:40

#include<bits/stdc++.h>
#define dep(i,e,s) for(int i=e; i>=s; --i)
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int a[50];
int main(){
	int _; cin>>_; while(_--){
		int n,n1,d(0),cnt(0); ll ans(0); cin>>n; n1=n;
		while(n1) a[++d]=n1&1,n1>>=1;
		while(n--) dep(i,d,1) if(a[i]) cnt++;
		else ans=(ans+1ll*cnt*(cnt-1)/2)%mod;
		cout<<ans<<'\n';
	}
}

C++14(g++5.4) 解法, 执行用时: 11ms, 内存消耗: 412K, 提交时间: 2020-01-19 21:31:04

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=1e5+5;
int a[N];
int main(){
	ll t,n,k,b,ans,cnt;
	cin>>t;
	while(t--){
		cin>>n,k=0,b=n,ans=0,cnt=0;
		while(n){ a[k++]=n&1,n>>=1;}
		while(b--){
			for(int i=k-1;i>=0;i--){
				 if(a[i]) cnt++;
				 else ans=(ans+cnt*(cnt-1)/2)%mod;
			}
		}
		cout<<ans<<endl;
	}
}

Python3(3.5.2) 解法, 执行用时: 1642ms, 内存消耗: 4296K, 提交时间: 2020-01-19 14:45:44

t=int(input())
mod=1000000007
for i in range(0,t):
    s=int(input())
    ans=bin(s)[2:]
    s1=0
    s2=0
    ss=ans
    for j in range(0,s-1):
        ans+=ss
    #print(ans)
    for j in range(0,len(ans)):
        if ans[j]=='1':
            s1=(s1+1)%1000000007
        if ans[j]=='0':
            s2=(s2+s1*(s1-1)//2)%1000000007
    print(s2%1000000007)

上一题