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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值