AtCoder Beginner Contest 161解题报告

题目链接

A. ABC Swap

题意:
a b a c 给三个数,交换a和b,交换a和c
题解:
s w a p 签到题,直接用swap操作就行

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};



int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int a,b,c;
    cin>>a>>b>>c;
    swap(a,b);
    swap(a,c);
    cout<<a<<' '<<b<<' '<<c<<endl;

    return 0;
}


B.Popular Vote

题意:
n m n a 给数n和m,和一个长度为n的数组a
a i a s u m / ( 4 m ) 如果a_i大于等于数组a的sum/(4*m)即可选取
m 问是否能选取出m个
题解:
s u m 4 m 读数计算和,然后用sum除4m并进一
a i s u m a i d o u b l e 由于a_i的值必须大于等于sum,而且a_i是整数,不需要用double的方法就是进一

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int a[110];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int n,m;
    cin>>n>>m;
    int sum=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        sum+=a[i];
    }
    sum=(sum+4*m-1)/(4*m);
    int ans=0;
    for(int i=1;i<=n;i++){
        if(a[i]>=sum)ans++;
    }
    if(ans>=m)cout<<"Yes";
    else cout<<"No";
    return 0;
}


C.Replacing Integer

题意:
n k n n k n 给数n和k,每次n变成n和k的差的绝对值,问n最小能变成多少
题解:
n k n k n 直接将n向k取余,取n和k-n的最小值
n k < = 1 e 18 由于n和k都是<=1e18数较大,取余相当于多段减法
去除了其中不必要的过程
n k n k n 取余后n肯定小于k,所以答案就在n和k-n中取

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int a[110];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ll n,k;
    cin>>n>>k;
    n%=k;
    cout<<min(n,k-n);

    return 0;
}


D.Lunlun Number

题意:
1 对任一个数字,相邻两位差的绝对值不大于1的则符合条件
k 问从小到大第k个符合条件的
题解
k < = 1 e 5 k<=1e5,并且样例给出此时的答案,数字较大,不可能暴力查数
1 e 5 所以每次直接找符合的数,最多找1e5次
B F S 这里采用BFS对数位进行查找,每次查最后一位的相邻数
1 0 + 1 0 9 即-1,0,+1的情况,特判一下0和9少其中一种情况

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    queue<ll> q;
    int n;
    cin>>n;
    int num=0;
    ll ans;
	for(int i=1;i<=9;i++) q.push(i);
	while(!q.empty()){
		ll p=q.front();
		q.pop();
        num++;
		if(num==n){ans=p;break;}
		int f=p%10;
		if(f==0){
			q.push(p*10);
			q.push(p*10+1);
		}
		else if(f==9){
            q.push(p*10+8);
            q.push(p*10+9);
		}
		else{
			q.push(p*10+f-1);
			q.push(p*10+f);
			q.push(p*10+f+1);
		}
	}
	cout<<ans;
    return 0;
}


E.Yutori

题意:
T a k a h a s h i n k Takahashi从明天开始需要从n天里选k天上班
n k c s 给你整数n,k,c,和一个字符串s
T a k a h a s h i 要符合以下两个条件Takahashi才去上班
1. c 1.距离上次上班已经过了c天
2. i s i x 2.第i天的s_i不能是x
T a k a h a s h i 问哪些天Takahashi确定他会去上班
题解:
直接采取贪心策略
c x 倒着循环每次最早能上班的时刻上班,然后找c天后的不是x的天
然后正着循环
T a k a h a s h i 这两个分别是Takahashi上班的左区间和右区间
只要这个区间最后聚在一个点,这一天就是确定的一天

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=2e5+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int l[maxn],r[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,k,c;
    cin>>n>>k>>c;
    string s;
    cin>>s;
    int cnt=k-1,mx=n;
    for(int i=s.length()-1;i>=0;i--)
        if(s[i]=='o'&&i<mx&&cnt>=0)r[cnt]=i,mx=i-c,cnt--;
    cnt=0,mx=-1;
    for(int i=0;i<s.length();i++)
        if(s[i]=='o'&&i>mx)l[cnt]=i,mx=i+c,cnt++;
    for(int i=0;i<k;i++)
        if(l[i]==r[i])cout<<l[i]+1<<endl;
    return 0;
}


F.Division or Substraction

题意:
n n < = 1 e 12 给一个数n(n<=1e12)
k n % k = = 0 n = n / k 存在一个k,如果n\%k==0,n=n/k
n = n k 否则n=n-k
k n 1 问有多少种k能让n最后变成1
题解:
n < = 1 e 12 ( ) 由于n<=1e12所以可以推测是用根号方法(找因子)
n % k = 0 n k 使 n k 如果n\%k!=0,n不断减k也不会使得n能被k整除
所以剩下的操作只有多段减法,就可以转换成取余
n 1 所以先找能让n不能整除,但是取余后直接等于1的
n 1 n % ( n 1 ) = 1 首先肯定是n-1,n\%(n-1)=1
n 1 然后根据取余的性质,n-1的因子肯定也可以符合
1 找完直接取余等于1的之后,就找能整除的
n 然后找n的因子直接先除到不能整除再取余模拟判断
最后答案加上自身
n 2 s q r t n 找n的因子肯定是2-sqrt(n)之间的
n 之后的因子n除之后肯定比他本身小,就不能进行下一步操作

AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ll n,t;
    cin>>n;
    int ans=0;
    for(ll i=1;i*i<=n-1;i++){
        if((n-1)%i==0){
            if(i!=1)ans++;
            if((n-1)/i!=i)ans++;
        }
    }

    for(ll i=2;i*i<=n;i++){
        if(n%i==0){
            t=n;
            while(t%i==0)t/=i;
            if(t%i==1)ans++;
        }
    }
    cout<<ans+1;
    return 0;
}

发布了12 篇原创文章 · 获赞 10 · 访问量 3098

猜你喜欢

转载自blog.csdn.net/qq_43756519/article/details/105319491
今日推荐