Codeforces round (Div. 2) personal solutions (A,B,C,D,E)

Question A:
Find out the maximum value of the greatest common divisor of any logarithm a and b in 1-n;
Train of thought: directly find divide by 2, water problem;

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		if(n%2==0)
			cout << n/2 <<endl;
		else
			cout << max((n-1)/2,1) <<endl;
	}
    return 0;
}

Question B:
Meaning: give a n array a with a length of 2n, and find an array b with a length of n-1 to meet the following conditions:
1. The elements of array b consist of the sum of any two numbers of a. (cannot select repeatedly)
2.gcd(b1,b2,......,bn-1)>1
Idea: just put all array b together into even numbers. (the subscript of the output, the game time wa a hair).

#include <bits/stdc++.h>
using namespace std;
 
int a[2010];
int b[2010];
 
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		int c1=0,c2=0;
 
		int v;
		for(int i=0;i<2*n;i++)
		{
			scanf("%d",&v);
			if(v%2==0) a[c1++]=i+1;
			else b[c2++]=i+1;
		}
		
		if(c2%2!=0)
		{
			c1--;c2--;
		}
		else
		{
			if(c2==0) c1-=2;
			else c2-=2;
		}
 
		for(int i=0;i<c2;i+=2)
			printf("%d %d\n",b[i],b[i+1]);
 
		for(int i=0;i<c1;i+=2)
			printf("%d %d\n",a[i],a[i+1]);
 
	}
    return 0;
}

Question C:
Meaning: two people play a game and give a number. They can perform the following operations:
1. Divide this number by an odd number greater than 1.
2. If the number is greater than 1, subtract the number by 1
When someone's turn doesn't work, he loses
Idea: when the number is odd, the first person will win. When the number is even, if the even factor of the number is maximum 2, and the odd factor cannot be split, the first person cannot win. If the number can be split or the maximum even factor is not 2, the first person must win.

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
 
		if(n==1||n==4||n==6) 
		{
			cout << "FastestFinger" <<endl;
			continue;
		}
		else if(n==2||n==3||n==5)
		{
			cout << "Ashishgup" <<endl;
			continue;
		}
 
		if(n%2!=0)
			cout << "Ashishgup" <<endl;
		else
		{
			int num=1;\\num Is the maximum even factor
			while(n%2==0)
			{
				num*=2;
				n/=2;
			}		
			if(n>1&&num>2)
				cout << "Ashishgup" <<endl;
			else if(n==1)
				cout << "FastestFinger" <<endl;
			else if(n>1&&num==2)
			{
				int flag=0;
				int m=sqrt(n);
				for(int i=3;i<=m;i+=2)\\See if odd factor can be split
				{
					if(n%i==0)
					{
						flag=1;
						break;
					}
				}
 
				if(flag)
					cout << "Ashishgup" <<endl;
				else
					cout << "FastestFinger" <<endl;
			}
 
		}
 
	}

    return 0;
}

Question D:
Problem meaning: give an array a, find a subsequence b to make its maximum value of odd term and minimum value of maximum value of even term minimum.
Thinking: dichotomy, simulation.

#include <bits/stdc++.h>
#define MAX 200005
using namespace std;

int a[MAX];
int n,k;
 
bool check(int x)
{
	int flag=0,cnt=0;
 
	for(int i=0;i<n;i++)
	{
		if(flag||x>=a[i])
		{
			cnt++;
			flag=!flag;
		}
	}
 
	if(cnt>=k)
		return true;
	cnt=0;flag=1;
	for(int i=0;i<n;i++)
	{
		if(flag||x>=a[i])
		{
			cnt++;
			flag=!flag;
		}
	}
 
	if(cnt>=k)
		return true;
 
	return false;
}
 
int main()
{
	cin >> n>> k;
	for(int i=0;i<n;i++) 
		scanf("%d",&a[i]);
 
	int l=1,r=1e9;
	int ans=0;
	while(l!=r)
	{
		int mid=(l+r)/2;
		if(check(mid))
		{
			r=mid;
			ans=mid;
		}
		else
			l=mid+1;
	}
	cout << ans <<endl;
 
    return 0;
}

Question E:
I'll give you two 01 strings a and b to find the minimum operands that make them equal. The executable operation is to select a string of a and move it parallel to the right (a1=an, a2=a1...)
Idea: direct simulation, regardless of the equal position.

#include <bits/stdc++.h>
using namespace std;

 
int main()
{
	int n;
	cin >> n;
	string s1,s2;
	cin >> s1 >> s2;
 
	int num=0;
	int c0=0,c1=0;
	int ans=0;
	for(int i=0;i<n;i++)
	{
		if(s1[i]==s2[i])
			continue;
 
		if(s1[i]=='1')
		{
			num++;
			if(c0>0)
			   c0--;
			c1++;
		}
		else
		{
			num--;
			if(c1>0)
				c1--;
			c0++;
		}
	}
	if(num!=0)
		cout << -1 <<endl;
	else
	    cout << c0+c1 <<endl;
	
    return 0;
}

Posted on Sun, 21 Jun 2020 06:41:48 -0400 by mosi