AT_arc208_b [ARC208B] Sum of Mod 题解

· · 题解

AT_arc208_b [ARC208B] Sum of Mod 题解

前言

讲一种不用二分 a_1 的做法

解法

为了方便描述,记题目中的式子的值为 sum

首先,因为取模,差最大为 a_{i-1}-1,所以我们要让 a_n 尽量小,就要最小化前面的值,所以每次 a_ia_{i-1}-1+a_{i-1}2 \times a_{i-1} -1 时最优。当后面 sum 的值等于 k 时,令后面全部相等即可。

考虑现在 a_1 取什么值,当 a_11 时,1 于后面任何数都没有贡献,还占了一个位置,一定不优,所以 a_12。但是这样可能一因为长度限制而凑不够 k,所以开始调整 a_1 的取值。

a_1 等于 2 时,序列为 2,3,5,9,\dotssum=(3-2)+(5-3)+(9-5)+\dots = 1+2+4+\dots=2^{n+1}-1n 为这一串累加的长度。

a_13 时,序列为 3,5,9,17,\dotssum'=(5-3)+(9-5)+(17-9)+\dots=2+4+8+\dots =2 \times sum

同理,当 a_i4 时,sum''=3 \times sum

所以 a_1=\lceil \frac {k}{sum} \rceil +1。 后面正常构造即可。

代码

#include<bits/stdc++.h>
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 1000000
    char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
    template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
    template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
    template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
    inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
    inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
    inline void print(char x) { putchar(x); }
    inline void print(char *x) { while (*x) putchar(*x++); }
    inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
    inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
    inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
    inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
    inline void print(bool b) { putchar(b+48); }
    template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
    template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
    struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
    template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
    template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=2e5+10;
int n,k;
int a[N];
void init()
{
    for(int i=1;i<=n;i++)
        a[i]=0;
}
void solve()
{
    cin>>n>>k;
    init();
    int d=2;
    if(n<=31)
    {
        int sum=pow(2,n-1)-1;
        d=max(2ll,(int)ceil((double)k/(double)sum)+1);
    }
    a[1]=d;
    for(int i=2;i<=n;i++)
    {
        if(k>=a[i-1]-1)
        {
            if(a[i-1]==1)
                a[i]=a[i-1]*2;
            else
                a[i]=a[i-1]*2-1;
            k-=(a[i-1]-1);
        }
        else
        {
            a[i]=a[i-1]+k;
            k=0;
        }
    }
    for(int i=1;i<=n;i++)
        cout<<a[i]<<" ";
    cout<<endl;
}
signed main()
{
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}