• Atcoder arc079 D Decrease (Contestant ver.) (逆推)


    D - Decrease (Contestant ver.)


    Time limit : 2sec / Memory limit : 256MB

    Score : 600 points

    Problem Statement

    We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N−1 or smaller.

    • Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

    It can be proved that the largest element in the sequence becomes N−1 or smaller after a finite number of operations.

    You are given an integer K. Find an integer sequence ai such that the number of times we will perform the above operation is exactly K. It can be shown that there is always such a sequence under the constraints on input and output in this problem.

    Constraints

    • 0≤K≤50×1016

    Input

    Input is given from Standard Input in the following format:

    K
    

    Output

    Print a solution in the following format:

    N
    a1 a2 ... aN
    

    Here, 2≤N≤50 and 0≤ai≤1016+1000 must hold.


    Sample Input 1

    Copy
    0
    

    Sample Output 1

    Copy
    4
    3 3 3 3
    

    Sample Input 2

    Copy
    1
    

    Sample Output 2

    Copy
    3
    1 0 3
    

    Sample Input 3

    Copy
    2
    

    Sample Output 3

    Copy
    2
    2 2
    

    The operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1].


    Sample Input 4

    Copy
    3
    

    Sample Output 4

    Copy
    7
    27 0 0 0 0 0 0
    

    Sample Input 5

    Copy
    1234567894848
    

    Sample Output 5

    Copy
    10
    1000 193 256 777 0 1 1192 1234567891011 48 425

     

    题意:

      要你输出一个长度为N数组,满足k次操作。每一次操作选择数组里面最大的一个,将这个数减去N,其他的数全部加1。当执行完k次操作后,数组里面的最大一个数要小于等于N-1。这k次操作中全部数都要大于等于0.(k <= 50* 1016 , N<=50)

    题解:

      我们知道在最后的时候最大的是N-1。那么我们可以构造一个[0, 49] 的N为50 的数组。

      你每次选择一个最小的数加50, 其他是数减1。很明显这个[0, 49] 的数组是符合的。

      当你执行了50次操作的时候,你会发现数组变成了[1, 50]。

      我们就可以 在[0, 49] 的数组上直接 加上 循环次数 ((k-1/50)+1) - 1。

      我们还剩下 k%N 次操作

      如果当k%N > 0 的时候,直接暴力这些操作,选择一个最小的数+N, 其他数--。

      如果k%N == 0。 因为是N的倍数,因为循环次数是算的向下完整的循环,k%N==0,会使全部数+1.

      (k = 50)   [0, 49] 上加上了 49/50+1-1 = 0;但是第50次就变成了[1, 50]。

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <algorithm>
     6 #include <cmath>
     7 #include <vector>
     8 #include <queue>
     9 #include <map>
    10 #include <stack>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 typedef unsigned long long uLL;
    15 #define ms(a, b) memset(a, b, sizeof(a))
    16 #define pb push_back
    17 #define mp make_pair
    18 #define eps 0.0000000001
    19 #define IOS ios::sync_with_stdio(0);cin.tie(0);
    20 const LL INF = 0x3f3f3f3f3f3f3f3f;
    21 const int inf = 0x3f3f3f3f;
    22 const int mod = 1e9+7;
    23 const int maxn = 50000+10;
    24 LL a[60];
    25 int main() {
    26 #ifdef LOCAL
    27     freopen("input.txt", "r", stdin);
    28 //        freopen("output.txt", "w", stdout);
    29 #endif
    30 //    IOS
    31     LL k;scanf("%lld", &k);
    32     if(k==0)    cout << "2
    1 1
    ";
    33     else{
    34         int N = 50;
    35         LL t = (k-1)/N + 1;
    36         for(int i = 1;i<=N;i++) a[i] = i-1;
    37         for(int i = 1;i<=N;i++) a[i] += (t-1);
    38         int hh = k%N;
    39         if(hh==0){
    40             for(int i = 1;i<=N;i++) a[i]++;
    41         }
    42         for(int i = 1;i<=hh;i++){
    43             for(int j = 1;j<=N;j++){
    44                 if(i==j)    a[i] += N;
    45                 else    a[j]--;
    46             }
    47         }
    48         printf("%d
    ", N);
    49         for(int i = 1;i<=N;i++)
    50             printf("%lld ", a[i]);
    51         printf("
    ");
    52     }
    53     return 0;
    54 }
    View Code

      

  • 相关阅读:
    Eventbus的功能
    Linux下xz与tar的区别
    IntelliJ IDEA出现:This file is indented with tabs instead of 4 spaces的问题解决
    IntelliJ IDEA设置properties文件显示中文
    oh-my-zsh官方教程
    Vim出现:_arguments:450: _vim_files: function definition file not found的问题解决
    Ubuntu 16.04下安装zsh和oh-my-zsh
    zsh与oh-my-zsh是什么
    Mac安装IntelliJ IDEA时快捷键冲突设置
    IntelliJ IDEA删除项目
  • 原文地址:https://www.cnblogs.com/denghaiquan/p/7271442.html
Copyright © 2020-2023  润新知