0211 cattle off solution to a problem

Euclid A-

The meaning of problems

Now, if it is known GCD (a, b) a total of n times recursively, find all possible a, b satisfying a> b> = 0 and a + b a minimal set of the sum of a and b. Meaning that it is recursive n times b is 0, for example, n is 0 is gcd (1,0) the answer is 1, if n is 1 is gcd (2,1) is 3, and if 2 is gcd (3,2 ) yi secondary side is gcd (2,1) in the first to (1,0) of the

Thinking

By the full description of the problem can be seen at all times the smallest Fibonacci number, gcd fn + 1, fn

Code

#include<iostream>
using namespace std;
int main()
{
	long long  f[1000];
	int n;
	
	f[1]=1;
	f[2]=2;
	for(int i=3;i<=100;i++) 
	 f[i]=f[i-1]+f[i-2];
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		if(n==0)
		 cout<<"1"<<endl; 
		else
		
		  cout<<f[n]+f[n+1]<<endl;
	}
	return 0;
	
}

B- bracket sequence

The meaning of problems

To a sequence in parentheses is legitimate to ask whether, () {} is legal, {()} is legal

Thinking

Use the stack, if left bracket on the stack, then the closing parenthesis encountered when first determine whether the stack is empty if the stack is empty, it is certainly not legitimate, such as the multi-stack is not empty, it took a stack of a given element and then delete the top of the stack, the stack determine when the left bracket and the right bracket now match, if the match to continue. If not quit, there is a final judgment to see when the stack is empty, is not empty does not work, because it is not empty the explanation also left parenthesis

Code

#include<iostream>
#include<string>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
	string str;
	cin>>str;
	int f=1;
	stack<char>s;
	for(int i=0;i<str.length();i++)
	{
		if(str[i]=='('||str[i]=='['||str[i]=='{')
		 s.push(str[i]);
		else
		 {
		 	if(s.empty())
		 	{
		 		f=0;
		 		break;
		 	}
		 	else
		 	 {
		 	 	char a=s.top();
		 	 	s.pop();
		 	 	if(str[i]==')')
		 	 	 if(a!='(')
		 	 	 {
		 	 	 	f=0;
		 	 	 	break;
		 	 	 }
		 	 	 if(str[i]=='}')
		 	 	  if(a!='{')
		 	 	 {
		 	 	 	f=0;
		 	 	 	break;
		 	 	 }
		 	 	 if(str[i]==']')
		 	 	  if(a!='[')
		 	 	 {
		 	 	 	f=0;
		 	 	 	break;
		 	 	 }
		 	 }
		 }
	}
	if(!s.empty()) 
	 f=0;
	if(f)
	 cout<<"Yes"<<endl;
	else
	 cout<<"No"<<endl;
	return 0;
}
	

C. Fields product

proposal

Kn and n is a given length of the array, then an array to you, let you ask continuous length k of the maximum product of the field but also a number of mold

Thinking

Foot emulated, fixed stationary l r jerk, when updating the maximum length is k, then A [l] was removed by his used herein inverse element, then jerk l, continues to repeat the above operation, it should be noted If the scan is going directly to the 0 l 0 is moved to the front,

Code

#include<iostream>
#include<algorithm> 
using namespace std;
const int mod=998244353;
typedef long long ll;
ll sum=1;
ll maxn=0;
ll a[200010];
ll quickpow(ll x,ll y)
{
	ll ans=1;
	while(y)
	{
		if(y&1)
		 ans=ans*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ans;
}
int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	 cin>>a[i];
	ll r=1,l=1;
	while(r<=n)
	{
		if(a[r]) //扫到的不是 0的情况
		{
			sum=(sum*a[r])%mod;
			if((r-l+1)%k==0)//长度为k时判断
			{
				if(sum>maxn) maxn=sum;
				sum=sum*quickpow(a[l],mod-2)%mod;//本来要除a[l]但wa 得乘al的逆元也就是然后根据费马小定理
				//逆元就是这样的求的 
				l++;
			} 
		} 
		else
		{
			l=r+1;
			sum=1;
		}//如果扫到0了就直接跳过 sum  也得为1 
		r++;
		 
	}
	cout<<maxn<<endl;
	return 0; 
	
	
}
 

D- fields XOR

The meaning of problems

To a length of n number of columns, and then a field [l, r] and the divergent or, as a [L] XOR a [l + 1] XOR a [l + 2] XOR up to a [R & lt ], and statistics for the number of the exclusive oR of the sub-segment 0, l and r are as long as one is different, so that different fields

Thinking

[1, l-1] ⊕ [1, r] = [l, r], so if [L, r] is 0, the prefix l-1 exclusive-OR and on and prefix doubt and equal to r, so it seeking a prefix and XOR, and then map maintenance

Code

#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
int a[200010],b[200010];
map<ll,ll>mp;
int main()
{
	int n;
	ll ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=b[i-1]^a[i];//求前缀异或和
		if(b[i]==0) ans++;//如果是 1,i 异或和为0也是满足要求的嘛
		ans+=mp[b[i]];
		mp[b[i]]++;//map 好像可以统计 b[i]的次数,先是为0,然后没出现一次就加1 
	}
	cout<<ans<<endl;
	return 0; 
}
发布了7 篇原创文章 · 获赞 0 · 访问量 95

Guess you like

Origin blog.csdn.net/xgx984826498/article/details/104358387