AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

Codeforces contest/1837/problem/A. A. Grasshopper on a Line    原题链接    简单

作者: 作者的头像   啼莺修竹 ,  2023-05-26 00:47:49 ,  所有人可见 ,  阅读 58


3


3
codeforce每日一题链接

题目描述

给你n,k,你要从0开始,怎样不是k的倍数的距离才能走到n,距离可以为负。

样例

输入样例1
3
10 2
10 3
3 4
输出样例1
2
7 3
1
10
1
3

算法1

(暴力枚举) $O(n^2)$

直接暴力枚举就好了。

C++ 代码

//  https://www.acwing.com/blog/content/34755/

#include<bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pd push_back

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<double, double> PDD;

const int N = 1e5 + 10, M = N * 2, INF = 0x3f3f3f3f;

int n, m;

inline void solve()
{
    cin>>n>>m;
    if(n==1) cout<<1<<endl<<1<<endl;
    else if(n%m!=0) cout<<1<<endl<<n<<endl;
    else
        for(int i=-1e4;i<=1e4;i++){
        if(i%m!=0 && (n-i)%m!=0){
            cout<<2<<endl;
            cout<<i<<' '<<n-i<<endl;
            return;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }

    return 0;
}

算法

(思维) $O(1)$

可以输出1和n-1.

C++ 代码

//  https://www.acwing.com/blog/content/34755/

#include<bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pd push_back

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<double, double> PDD;

const int N = 1e5 + 10, M = N * 2, INF = 0x3f3f3f3f;

int n, m;

inline void solve()
{
    cin>>n>>m;
    if(n==1) cout<<1<<endl<<1<<endl;
    else if(n%m!=0){
        cout<<1<<endl<<n<<endl;
    }else{
        cout<<2<<endl;
        cout<<1<<" "<<n-1<<endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }

    return 0;
}

2 评论


用户头像
Jiangly_fan   6天前         踩      回复

直接判 x % k 不是更简洁更快吗

用户头像
啼莺修竹   6天前         踩      回复

可以的,最后一个else可以直接输出1和n-1


你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息