First training match

Contest link

Question A
Idea: Consider A and B in binary.

Same bit for A and B

If it is 1 0, then no matter which bit the corresponding bit of x takes, the final result is still 1.

If it is 0 1, then no matter which bit the corresponding bit of x takes, the final result is still 1.

If it is 0 0, then x only needs to take 0 corresponding to this bit and add it to 0

If it is 1 1, then x only needs to take 1 corresponding to this bit and add up to 0

So that is to say 1 0–>1;0 1—>1;0 0—>0;1 1—>0; then the final result is a^b.

Question B
Idea: Enumeration only needs to ensure that the two adjacent grids at the start and end points are different

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n, m, _;
char x[210][210];
void solve() {
    
    
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> x[i] + 1;
    int 
	a = x[1][2] - '0',          //  Sa
	b = x[2][1] - '0', 			//	b
	c = x[n][n - 1] - '0',		//		 d
    d = x[n - 1][n] - '0';		//		cF
    
    if (a == b && c == d && a != c)
        cout << 0 << endl;
    else if (a != b && c != d)
        cout << 2 << endl
             << 1 << " " << 2 << endl  // 翻转 a 
             << (a == c ? n - 1 : n) << " " << (a == c ? n : n - 1) << endl;
             //  注意  实际并没有改变a的值   如果c和反转前的a相等   就反转d 
			 //  假设  S1
			 //		   0
			 //				0
			 //			   1F  
    else if (a != b)
        cout << 1 << endl
             << (a == c ? 1 : 2) << ' ' << (a == c ? 2 : 1) << endl;
    else if (c != d)
        cout << 1 << endl
             << (a == c ? n : n - 1) << ' ' << (a == c ? n - 1 : n) << endl;
    else
        cout << 2 << endl << 1 << ' ' << 2 << endl << 2 << " " << 1 << endl;
}
int main() {
    
    
    cin >> _;
    while (_--) solve();
    return 0;
}


Question C

Construction problems
often find that you need to simulate by yourself to find the general solution and then directly output
Insert picture description here

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+5;
char s[N];
int len;
int main()
{
    
    
    scanf("%s",s);
    len=strlen(s);
    puts("4");
    puts("L 2");
    puts("L 2");
    puts("R 2");
    printf("R %d\n",2*len+1);
    return 0;
}

The second structure

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
int main() {
    
    
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> s;
    cout << "3\n";
    cout << "L 2\n";
    cout << "R 2\n";
    cout << "R " << 2 * s.size() - 1 << endl;
}


Question H (HDU 1559)

First use the prefix sum to find it,
and then traverse all the sub-matrices of x*y size.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int maze[1009][1009];
int t,m,n,x,y;
int main() {
    
    

    cin >> t;
    while(t--)
    {
    
    
        scanf("%d %d %d %d",&m, &n, &x, &y);
        memset(maze,0,sizeof(maze));

        for(int i=1; i<=m; i++)
        {
    
    
            for(int j=1; j<=n; j++)
            {
    
    
                scanf("%d",&maze[i][j]);
                maze[i][j]=maze[i][j]+maze[i-1][j]+maze[i][j-1]-maze[i-1][j-1];
            }
        }
        int cnt=0;
        for(int i=x; i<=n; i++)
        {
    
    
            for(int j=y; j<=m; j++)
            {
    
    
                cnt=max(cnt,maze[i][j]+maze[i-x][j-y]-maze[i-x][j]-maze[i][j-y]);
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

F (UVA 10815)
F question
string question

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct z
{
    
    
	char s[209];
}zm[500009];
int cnt,t;//  cnt 计单词  t计字母
char c;
bool cmp(z a, z b)
{
    
    
	return strcmp(a.s,b.s)<0;
}
int main()
{
    
    
	while((c=getchar())!=EOF)//   用%s输入不好判定输入时标点的位置
	{
    
    
		if(c>='A'&&c<='Z') c+=32;
		
		if(c>='a'&&c<='z'){
    
    
			zm[cnt].s[t++]=c;
		}
		else if(!(c>='a'&&c<='z')&&zm[cnt].s[0]>='a'&&zm[cnt].s[0]<='z'){
    
    
			zm[cnt].s[t]=0;
			cnt++,t=0;
		}
	}
	sort(zm,zm+cnt,cmp);
	cout << zm[0].s << endl;
	for(int i=1;i<cnt;i++)
	{
    
    
		if(strcmp(zm[i].s,zm[i-1].s)) printf("%s\n",zm[i].s);	//去重
	} 	
	return 0;
}

Question K

One-dimensional difference

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,l,r;
int f[100009];
int a[100009];
int main(){
    
    

    while(scanf("%d",&n),n){
    
    
        memset(f,0,sizeof(f));
        for(int i=1;i<=n;i++){
    
    
            scanf("%d%d",&l,&r);
            f[l]+=1;
            f[r+1]-=1;
        }
        for(int i=1;i<=n;i++)
            a[i]=a[i-1]+f[i];

        for(int i=1;i<=n;i++)
           {
    
    
               printf("%d",a[i]);
               if(i!=n) printf(" ");
           }
        printf("\n");

    }
    return 0;
}

Question M (HDU 5428)

Idea: For each number, its useful part is actually all its prime factors (including equal).
Find the smallest two of all prime factors of all numbers, and multiply that is the answer.
If the number of prime factors of all numbers is less than two, then there is no solution.
Time complexity O(n*sqrt(a))O(n∗sqrt(a))

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll Max = 100000;
ll a[110];
ll count1[123456];

int main(){
    
    
    int t;
    scanf("%d",&t);
    int n;
    while(t--)
    {
    
    
        scanf("%d",&n);
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
    
    
            scanf("%lld",&a[i]);
            for(ll j=2; j*j <= a[i]; j++) // 注意取等 或者  j<=sqrt(a[i])
            {
    
    
                while(a[i] % j == 0)
                {
    
    
                    count1[cnt++] = j;
                    a[i] /= j;
                }
            }
            if(a[i]!=1)
                count1[cnt++]=a[i];
        }
        if(cnt < 2)   printf("-1\n");
        else{
    
    
            sort(count1,count1+cnt);
            printf("%lld\n",count1[0]*count1[1]);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/cosx_/article/details/112857751