zoukankan      html  css  js  c++  java
  • [Codeforces]1263C Everyone is a Winner!

    题目

    On the well-known testing system MathForces, a draw of nn rating units is arranged. The rating will be distributed according to the following algorithm: if kk participants take part in this event, then the nn 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=5 and k=3k=3, then each participant will recieve an 11 rating unit, and also 22 rating units will remain unused. If n=5n=5, and k=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=5, then the answer is equal to the sequence 0,1,2,50,1,2,5. Each of the sequence values (and only them) can be obtained as ⌊n/k⌋ for some positive integer kk (where x⌊x⌋ is the value of xx rounded down): 0=5/7,1=5/5,2=5/2,5=5/10=⌊5/7⌋, 1=⌊5/5⌋, 2=⌊5/2⌋, 5=⌊5/1⌋.

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

    输入

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

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

    输出

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

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

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

    题目大意

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

    想法

    朴素想法:枚举1n1-n,每个除一遍,加到setset中去重,然后直接遍历输出即可。
    复杂度O(tnlogn)O(tnlog n),死路一条。

    考虑到x×x=nx imes x=n,我们可以最多枚举到nsqrt n,在枚举时同时加入xxn/xn/x,那么式子看起来是这样:
    n÷(n÷x)=xn div (n div x) = x

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

    即可保证所有整除商都被加入setset中。
    此时复杂度O(tnlogn)O(tsqrt nlog n),能过。
    代码如下:

    #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
    ",out.size());
            for (set<int>::iterator it = out.begin(); it != out.end(); ++it)
                printf("%d ", *it);
            printf("
    ");
        }
        return 0;
    }
    

    但事实上,还有更简单的方法:我们可以去掉这个setset
    在枚举x=[1,n]x = [1,sqrt n]时,我们会发现,每个xx都是可以取到且不重复的,而n÷xn div x实际上也是不重复的。证明如下:
    n÷x1=k1,n÷x2=k2x1>x2设n div x_1 = k_1,n div x_2 = k_2,其中x_1 > x_2
    则有:
    k1×x1+t1=n,t1[0,x1)k_1 imes x_1 + t_1 = n,t1 in [0,x_1)
    k2×x2+t2=n,t2[0,x2)k_2 imes x_2 + t_2 = n,t2 in [0,x_2)
    假如k1=k2=kk_1 = k_2 = k,那么:
    k×x1+t1=k×x2+t2k imes x_1 + t_1 = k imes x_2 + t_2
    k×(x1x2)=t2t1k imes (x_1 - x_2) = t_2 - t_1
    k=(t2t1)(x1x2)k = frac{(t_2 - t_1)}{(x_1 - x_2)}
    然而:
    t2t1(x1,x2x1)t_2 - t_1 in (-x_1,x_2-x_1)
    那么k(x1x1x2,x2x1x1x2)k in (frac{-x_1}{x_1 - x_2},frac{x_2-x_1}{x_1-x_2})
    显然此时k<0k<0,产生了矛盾。

    因此,对于x[1,n]x in [1,sqrt n],我们得到的所有的xxn÷xn div x即为答案。
    顺序枚举xx,将n÷xn div x存入另一个数组中,显然该数组中的数单调递减。
    还需要特判最后x=nx = sqrt n时,x=n÷xx = n div x的情况。
    输出答案直接输出[0,n][0,sqrt n],再逆序输出保存数组中的结果即可。

    复杂度O(tn)O(tsqrt n),已经相当优秀了。
    还有一种整除分块的方法,本蒟蒻还不会……

    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
    ", cnt + lim + 1);
            for (int i = 0; i <= lim; ++i)
                printf("%d ", i);
            for (int i = cnt; i; --i)
                printf("%d ", save[i]);
            putchar('
    ');
        }
        return 0;
    }
    
  • 相关阅读:
    发现一个github上特别优秀的面试准备资料
    坠吼的大哥的博客
    [BJDCTF 2nd]假猪套天下第一
    [网鼎杯 2020 朱雀组]phpweb
    [GWCTF 2019]我有一个数据库
    [BJDCTF2020]ZJCTF,不过如此
    [GXYCTF2019]禁止套娃
    洛谷
    [ZJCTF 2019]NiZhuanSiWei
    蓝帽杯决赛-爆炒腰花-WP
  • 原文地址:https://www.cnblogs.com/Clouder-Blog/p/12146646.html
Copyright © 2011-2022 走看看