AtCoder题解——AtCoder Beginner Contest 180——C - Cream puff

题目相关

题目链接

AtCoder Beginner Contest 180 C 题,https://atcoder.jp/contests/abc180/tasks/abc180_c

Problem Statement

We have N cream puffs.

Find all possible number of people to which we can evenly distribute the cream puffs without cutting them.

Input

Input is given from Standard Input in the following format:

N

Output

Print the numbers of people in ascending order, each in its own line.

Samples1

Sample Input 1

6

Sample Output 1

1
2
3
6

Explaination

For example, we can evenly distribute the cream puffs to two people by giving three to each person.

Samples2

Sample Input 2

720

Sample Output 2

1
2
3
4
5
6
8
9
10
12
15
16
18
20
24
30
36
40
45
48
60
72
80
90
120
144
180
240
360
720

Samples3

Sample Input 3

1000000007

Sample Output 3

1
1000000007

Constraints

  • 1 \leq N \leq 10^{12}
  • N is an integer.

题解报告

题目翻译

给 N 个泡芙,请写出所有分法,泡芙不能切开。从小到大输出。

题目分析

惭愧,发现 AtCoder Grand Contest 已经肝不动了。还是混混鱼塘比较自在一点。

本题就是求数字 N 的因数。本题的核心是以下这么几个:

1、循环的结束在哪里?如果循环到 N,由于 N 很大,这样肯定是 TLE。可以参考标准质数判断的方法,循环到平方即可;

2、数据保存?可能考点就在这里,我们可以使用 STL 的 set,set 会自动排序。当然也可以保存到 vector,然后再排序。

3、注意变量的数据类型。由于 N 最大为 10^12,所以需要使用 long long。

AC 参考代码

//https://atcoder.jp/contests/abc180/tasks/abc180_c
//C - Cream puff
#include <iostream>
#include <set>
using namespace std;
int main() {
    long long n;
    cin>>n;

    set<long long> ans;
    for (long long i=1; i*i<=n; i++) {
        if (0==n%i) {
            ans.insert(i);
            ans.insert(n/i);
        }
    }

    set<long long>::iterator it;
    for(it=ans.begin(); it!=ans.end(); it++) {
        cout<<*it<<"\n";
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力的老周

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

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

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

打赏作者

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

抵扣说明:

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

余额充值