Codeforces Round #655 (Div. 2)题解

A. Omkar and Completion

题目链接

https://codeforces.com/contest/1372/problem/A

思路

只需要满足表达式在这里插入图片描述
直接输出n个1即可

代码

#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
        int n;
        cin>>n;
        while(n--)
            cout<<"1 ";
        cout<<endl;
    }
    return 0;
}

B. Omkar and Last Class of Math

题目链接

https://codeforces.com/contest/1372/problem/B

思路

1、n为偶数时,两数即为n/2
2、n为奇数时,n被分成a和b,
令a<=b<n,很明显1和n-1的LCM为n-1<n(n为质数的情况),又 LCM(a,b)>=b,所以 LCM(a,b)=b,
即为求n的除自己外的最大因子

代码

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n&1){
            int flag=1;
            for(int i=2; i*i<=n; i++){
                if(n%i==0){
                    flag=0;
                    cout<<n/i<<" "<<n-n/i<<endl;
                    break; 
                }
            }
            if(flag)
                cout<<"1 "<<n-1<<endl;
        }
        else
            cout<<n/2<<" "<<n/2<<endl;
    }
    return 0;
}

C. Omkar and Baseball

题目链接

https://codeforces.com/contest/1372/problem/C

思路

分三种情况:
1、已排好序,为0
2、子数组元素全部乱序,为1
3、子数组元素部分位置正确,为2

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, a[200009]={0}, b ,ans=0;
        cin>>n;
        int flag1=1;
        for(int i=1; i<=n; i++){
            cin>>b;
            if(b==i)
                a[b]=1;
        }
        for(int i=1; i<=n; i++){
            if(flag1&&a[i]==0){
                ans++;
                flag1=0;
            }
            if(!flag1 && a[i]==1)
            {
                flag1=1;
            }
        }
        if(ans==0)
            cout<<"0"<<endl;
        else if (ans==1)
            cout<<"1"<<endl;
        else
            cout<<"2"<<endl;      
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xmyrzb/article/details/107302097