题目

On the well-known testing system MathForces, a draw of nnn rating units is arranged. The rating will be distributed according to the following algorithm: if kkk participants take part in this event, then the nnn rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain — it is not given to any of the participants.

For example, if n=5n=5n=5 and k=3k=3k=3, then each participant will recieve an 111 rating unit, and also 222 rating units will remain unused. If n=5n=5n=5, and k=6k=6k=6, then none of the participants will increase their rating.

Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.

For example, if n=5n=5n=5, then the answer is equal to the sequence 0,1,2,50,1,2,50,1,2,5. Each of the sequence values (and only them) can be obtained as ⌊n/k⌋ for some positive integer kkk (where ⌊x⌋⌊x⌋⌊x⌋ is the value of xxx rounded down): 0=⌊5/7⌋,1=⌊5/5⌋,2=⌊5/2⌋,5=⌊5/1⌋0=⌊5/7⌋, 1=⌊5/5⌋, 2=⌊5/2⌋, 5=⌊5/1⌋0=⌊5/7⌋,1=⌊5/5⌋,2=⌊5/2⌋,5=⌊5/1⌋.

Write a program that, for a given nnn, finds a sequence of all possible rating increments.

输入

The first line contains integer number t(1≤t≤10)t (1≤t≤10)t(1≤t≤10) — the number of test cases in the input. Then ttt test cases follow.

Each line contains an integer n(1≤n≤109)n(1≤n≤10^9)n(1≤n≤109) — the total number of the rating units being drawn.

输出

Output the answers for each of ttt test cases. Each answer should be contained in two lines.

In the first line print a single integer mmm — the number of different rating increment values that Vasya can get.

In the following line print mmm integers in ascending order — the values of possible rating increments.

题目大意

给定ttt组数据,每组包含一个整数n(1≤n≤109)n(1≤n≤10^9)n(1≤n≤109),求出所有n被整除能得出的商。

想法

朴素想法:枚举1−n1-n1−n,每个除一遍,加到setsetset中去重,然后直接遍历输出即可。

复杂度O(tnlog⁡n)O(tn\log n)O(tnlogn),死路一条。

考虑到x×x=nx\times x=nx×x=n,我们可以最多枚举到n\sqrt nn​,在枚举时同时加入xxx和n/xn/xn/x,那么式子看起来是这样:

n÷(n÷x)=xn \div (n \div x) = xn÷(n÷x)=x

n÷x=n÷xn \div x = n \div xn÷x=n÷x

即可保证所有整除商都被加入setsetset中。

此时复杂度O(tnlog⁡n)O(t\sqrt n\log n)O(tn​logn),能过。

代码如下:

#include <cstdio>
#include <set>
#include <cmath>
using namespace std;
int t, n;
set<int> out;
int main()
{
scanf("%d", &t);
while (t--)
{
out.clear();
scanf("%d", &n);
out.insert(0);
int lim = sqrt(n);
for (int i = 1; i <= lim; ++i)
{
out.insert(i);
out.insert(n / i);
}
printf("%d\n",out.size());
for (set<int>::iterator it = out.begin(); it != out.end(); ++it)
printf("%d ", *it);
printf("\n");
}
return 0;
}

但事实上,还有更简单的方法:我们可以去掉这个setsetset!

在枚举x=[1,n]x = [1,\sqrt n]x=[1,n​]时,我们会发现,每个xxx都是可以取到且不重复的,而n÷xn \div xn÷x实际上也是不重复的。证明如下:

设n÷x1=k1,n÷x2=k2,其中x1>x2设n \div x_1 = k_1,n \div x_2 = k_2,其中x_1 > x_2设n÷x1​=k1​,n÷x2​=k2​,其中x1​>x2​

则有:

k1×x1+t1=n,t1∈[0,x1)k_1 \times x_1 + t_1 = n,t1 \in [0,x_1)k1​×x1​+t1​=n,t1∈[0,x1​)

k2×x2+t2=n,t2∈[0,x2)k_2 \times x_2 + t_2 = n,t2 \in [0,x_2)k2​×x2​+t2​=n,t2∈[0,x2​)

假如k1=k2=kk_1 = k_2 = kk1​=k2​=k,那么:

k×x1+t1=k×x2+t2k \times x_1 + t_1 = k \times x_2 + t_2k×x1​+t1​=k×x2​+t2​

k×(x1−x2)=t2−t1k \times (x_1 - x_2) = t_2 - t_1k×(x1​−x2​)=t2​−t1​

k=(t2−t1)(x1−x2)k = \frac{(t_2 - t_1)}{(x_1 - x_2)}k=(x1​−x2​)(t2​−t1​)​

然而:

t2−t1∈(−x1,x2−x1)t_2 - t_1 \in (-x_1,x_2-x_1)t2​−t1​∈(−x1​,x2​−x1​)

那么k∈(−x1x1−x2,x2−x1x1−x2)k \in (\frac{-x_1}{x_1 - x_2},\frac{x_2-x_1}{x_1-x_2})k∈(x1​−x2​−x1​​,x1​−x2​x2​−x1​​)

显然此时k<0k<0k<0,产生了矛盾。

因此,对于x∈[1,n]x \in [1,\sqrt n]x∈[1,n​],我们得到的所有的xxx和n÷xn \div xn÷x即为答案。

顺序枚举xxx,将n÷xn \div xn÷x存入另一个数组中,显然该数组中的数单调递减。

还需要特判最后x=nx = \sqrt nx=n​时,x=n÷xx = n \div xx=n÷x的情况。

输出答案直接输出[0,n][0,\sqrt n][0,n​],再逆序输出保存数组中的结果即可。

复杂度O(tn)O(t\sqrt n)O(tn​),已经相当优秀了。

还有一种整除分块的方法,本蒟蒻还不会……

Code

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int t, n, lim, cnt;
int save[50000];
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
lim = sqrt(n);
cnt = 0;
for (register int i = 1; i <= lim; ++i)
save[++cnt] = n / i;
if (cnt && lim == save[cnt]) //特判,注意有可能输入为0,这样cnt会被减为负数……
--cnt;
printf("%d\n", cnt + lim + 1);
for (int i = 0; i <= lim; ++i)
printf("%d ", i);
for (int i = cnt; i; --i)
printf("%d ", save[i]);
putchar('\n');
}
return 0;
}

[Codeforces]1263C Everyone is a Winner!的更多相关文章

  1. Codeforces Beta Round #2 A. Winner 水题

    A. Winner 题目连接: http://www.codeforces.com/contest/2/problem/A Description The winner of the card gam ...

  2. Codeforces Beta Round #2 A. Winner

    A. Winner time limit per test 1 second memory limit per test 64 megabytes input standard input outpu ...

  3. [Codeforces] #603 (Div. 2) A-E题解

    [Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...

  4. 『题解』Codeforces2A Winner

    Portal Portal1: Codeforces Portal2: Luogu Description The winner of the card game popular in Berland ...

  5. codeforces 2A Winner (好好学习英语)

    Winner 题目链接:http://codeforces.com/contest/2/problem/A ——每天在线,欢迎留言谈论. 题目大意: 最后结果的最高分 maxscore.在最后分数都为 ...

  6. CodeForces 2A - Winner(模拟)

    题目链接:http://codeforces.com/problemset/problem/2/A A. Winner time limit per test 1 second memory limi ...

  7. CodeForces 2A Winner

    Winner Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. Codeforces 2A :winner

    A. Winner time limit per test 1 second memory limit per test 64 megabytes input standard input outpu ...

  9. codeforces Winner

    /* * Winner.cpp * * Created on: 2013-10-13 * Author: wangzhu */ /** * 先找出所有选手的分数和中最大的分数和,之后在所有选手的分数和 ...

随机推荐

  1. Python磁力获取器命令行工具 torrent-cli

    作为一个搞代码的,找资源这种事肯定不能像普通人一样打开百度盲目查找,你需要写个爬虫工具来帮你完成这件事情啦! 兼容环境 Windows/Linux/MacOs 安装 pip 安装 $ pip inst ...

  2. ElementUI 删除 el-table 当前选中行(不是selection列)

    一句话即可: this.表格绑定的data.splice(this.$refs.表格的ref.store.states.selection, 1)

  3. Python 中命令行参数解析工具 docopt 安装和应用

    什么是 docopt? 1.docopt 是一种 Python 编写的命令行执行脚本的交互语言. 它是一种语言! 它是一种语言! 它是一种语言! 2.使用这种语言可以在自己的脚本中,添加一些规则限制. ...

  4. scrapy-redis分布式

    scrapy是python界出名的一个爬虫框架,提取结构性数据而编写的应用框架,可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 虽然scrapy 能做的事情很多,但是要做到大规模的分 ...

  5. win10+anaconda安装tensorflow和keras遇到的坑小结

    win10下利用anaconda安装tensorflow和keras的教程都大同小异(针对CPU版本,我的gpu是1050TI的MAX-Q,不知为啥一直没安装成功),下面简单说下步骤. 一 Anaco ...

  6. Java中四种遍历Map对象的方法

    方法一:在for-each循环中使用entry来遍历,通过Map.entrySet遍历key和value,这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Int ...

  7. linux面试经验

    互联网面试想必是每个学计算机的学生必不可少的环节,无论你的项目经验再多,你不准备基础知识,也还是无济于事.首先来说说关于工作的事情. 三年前,那时候我还是刚刚快要大四毕业的小鲜肉,那时候有个超大的招聘 ...

  8. Python 基础之import导包

    首先需要将import内容建立一个大概如下层级的包: 以黑色框为第一级,蓝色框为第二级,棕色框为第三级,红色框为第四级 一.import 引入初识 首先在module.py写入代码如下: xboy = ...

  9. springboot 时间戳和 数据库时间相差14个小时

    在 springboot 开发过程中遇到一个奇怪的问题,就是已经设置系统时间GMT+8, 但是时间到数据库后会减少14个小时.后来发现是 jvm 时区和数据库时区设置不一致的问题. jvm 设置的是 ...

  10. 搭建 nginx + rtmp 媒体服务器笔记

    工作需要搭建一个流媒体服务器,用来接收前端推过来的视频流,达到实时保存的目的. 具体步骤网上已经比较详细了 可以参考下面这个文档参考文档 https://www.cnblogs.com/monjeo/ ...