AtCoder题解 —— AtCoder Regular Contest 110 —— A - Redundant Redundancy

题目相关

题目链接

AtCoder Regular Contest 110 A 题,https://atcoder.jp/contests/arc110/tasks/arc110_a

Problem Statement

We have an integer N.

Print an integer x between N and 10^{13} (inclusive) such that, for every integer y between 2 and N (inclusive), the remainder when x is divided by y is 1 .

Under the constraints of this problem, there is always at least one such integer x.

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer x between N and 10^{13} (inclusive) such that, for every integer y between 2 and N (inclusive), the remainder when x is divided by y is 1.

If there are multiple such integers, any of them will be accepted.

Samples1

Sample Input 1

3

Sample Output 1

7

Explaination

The remainder when 7 is divided by 2 is 1, and the remainder when 7 is divided by 3 is 1, too.

7 is an integer between 3 and 10^{13}, so this is a desirable output.

Samples2

Sample Input 2

10

Sample Output 2

39916801

Constraints

  • All values in input are integers.
  • 2≤N≤30

题解报告

题目翻译

有一个整数 N。给出一个整数 x,x\in [N, 10^{13}],要求每一个整数 y \in [2, N],x 对 y 的余数都是 1。请找出这个 x。

题目分析

又是一个友善的打卡数学题。阅读题目后,我们知道就是一个找 2, 3, ..., N 的最小公倍数,然后将这个数据加一即可。

数据范围估计

x\in [N, 10^{13}],因此需要使用 long long 来表示。

也正是因为 MAXX=10^{13},如果我们使用暴力枚举的方法,即枚举 [N, 10^{13}] 这个范围,将导致 TLE,因为数据太大了。

AC 代码

//https://atcoder.jp/contests/arc110/tasks/arc110_a
//A - Redundant Redundancy
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

typedef long long LL;

const LL MAXX=1e13;

int main() {
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif

    LL n;
    cin>>n;

    LL x=2;
    for (LL y=x+1; y<=n; y++) {
        LL t=__gcd(x, y);
        x=x/t*y;
    }

    cout<<x+1<<"\n";

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

注意一个小细节,求 LCM 的时候,我们先除以 GCD,再乘。这样的目的是为了防止数据越界。

时间复杂度

O(N)。

空间复杂度

O(1)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值