Binary title

Portal: summing

Here Insert Picture Description

Resolution:

Consider first a 1 put in the position 1-n, the i-th position is assumed to put the first one, there are such numbers: 2 2 n-I-th (n-1 bits and placed behind a discharge or 0), the answer is 2 2 lowbit (i)the sum, then a first number 1 in the i of a contribution to the answer is 2 2 i-1then all contributions is the i-th 2 2 n-1, n positions of all contributions is: n 2 n*2 N-1 , 2 n 2^n 1 in the n + 1 bits, then the contribution is 2 n 2^n . The answer is: n 2 n*2 N-1 + 2 n +2^n .

Code:

#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353;
int read(long long &x){
	long long s=0,w=1;
	char c=getchar();
	if(c=='-') w=-1,c=getchar();
	while(c<='9'&&c>='0') s=s*10+c-'0',c=getchar();
	x=s*w;
}
long long  qp(long long  x,long long  y){
	long long  ans=1,tmp=x;
	while(y){
		if(y&1) ans=(ans%mod*tmp%mod)%mod;
		tmp=(tmp%mod*tmp%mod)%mod;
		y/=2;
	}
	return ans;
}
int main(){
	long long T;
	read(T);
	while(T--){
		long long n;
		read(n);
		long long n1=qp(2,n-1);
		long long n2=qp(2,n);
		printf("%lld\n",(((n%mod)*(n1%mod))%mod+n2)%mod);
	}
    return 0;
}

Portal: Unlock Expert

Meaning of the title: 01 statistical n-bit string, there may be no consecutive how many 1 (for example, n = 3, (011) does not comply, (010) accord).
Analysis: There are a 0,1 are two, two and 00 there are two, three have 001,010,000,100,101. The first three can be found in all three it is possible by adding the first two get 0, followed by two is obtained in the third bit plus 1 bit. So this is a Fibonacci entitled may not allow all-zero bit string, so again -1.

#include<bits/stdc++.h>
using namespace std;
const int mod=433494437;
long long p[110];
int main(){
    long long n,ans=1;
    p[0]=0;
    p[1]=2;p[2]=3;
    for(int i=3;i<=110;++i){
        p[i]=(p[i-1]+p[i-2])%mod;
    }
    while(cin>>n){
        cout<<(p[n]-1+mod)%mod<<endl;//当p[n]=0时如果不+mod答案会为负数,难怪Mod数不是1e9+7
    }
     
    return 0;
}
Published 96 original articles · won praise 11 · views 2272

Guess you like

Origin blog.csdn.net/weixin_43769146/article/details/103589604