SUBPRB - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Practice

Setter: Surya Prakash
Tester: Tejas Pandey and Utkarsh Gupta
Editorialist: Taranpreet Singh

DIFFICULTY

Simple

PREREQUISITES

Basic Math

PROBLEM

Given an integer N, construct an array A containing N distinct elements in range [1, 10^9] such that for each subarray A_{L, R}, the sum \displaystyle\sum_{i = L}^R A_i is divisible by length of subarray, i.e. (R-L+1).

QUICK EXPLANATION

Any arithmetic sequence with a common difference of a multiple of 2 would do.

EXPLANATION

Since this is a constructive problem, there may be many possible solutions. I’d share the one setting panel used, and prove why it works.

Let’s consider an arithmetic sequence with first term a and common difference d. The sum of first n terms of this sequence would be a*n + d*n*(n-1)/2. The term a*n is always a multiple of n, so we can choose any start term. We need n*d*(n-1)/2 to be a multiple of n, which can happen only when d*(n-1)/2 is an integer. Let’s choose d = 2. This way, the term a*n + d*n*(n-1)/2 is guaranteed to be a multiple of n.

This way, we can choose the array A such that A_i = a + d*i, where d is even, and 1 \leq a+d*i \leq 10^9. Any subarray of this A would be an arithmetic sequence with some start term and the common difference of a multiple of 2.

Hence, all subarrays would have sum a multiple of the length of the subarray, thus solving this problem.

Do share in the comments if you used some other construction based on a different idea.

TIME COMPLEXITY

The time complexity is O(N) per test case since we need to output N integers.

SOLUTIONS

Tester's Solution 1

#include <bits/stdc++.h>
using namespace std;

/*
------------------------Input Checker----------------------------------
*/

long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g==β€˜-’){
assert(fi==-1);
is_neg=true;
continue;
}
if(β€˜0’<=g && g<=β€˜9’){
x*=10;
x+=g-β€˜0’;
if(cnt==0){
fi=g-β€˜0’;
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

        assert(!(cnt>19 || ( cnt==19 && fi>1) ));
    } else if(g==endd){
        if(is_neg){
            x= -x;
        }

        if(!(l <= x && x <= r))
        {
            cerr << l << ' ' << r << ' ' << x << '\n';
            assert(1 == 0);
        }

        return x;
    } else {
        assert(false);
    }
}

}
string readString(int l,int r,char endd){
string ret=β€œβ€;
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
return readInt(l,r,’ β€˜);
}
long long readIntLn(long long l,long long r){
return readInt(l,r,’\n’);
}
string readStringLn(int l,int r){
return readString(l,r,β€˜\n’);
}
string readStringSp(int l,int r){
return readString(l,r,’ ');
}

/*
------------------------Main code starts here----------------------------------
*/

const int MAX_T = 1000;
const int MAX_N = 1000;
const int MAX_SUM_LEN = 5000;

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define mp make_pair
#define mt make_tuple
#define ll long long
const ll LIM = 1000000000;
#define pll pair<ll ,ll>
#define tll tuple<ll, ll, ll>

int sum_len = 0;
int max_n = 0;
int yess = 0;
int nos = 0;
int total_ops = 0;

void solve() {
int n;
n = readIntLn(1, MAX_N);
sum_len += n;
assert(sum_len <= MAX_SUM_LEN);
int cd = 100000, st = 1;
for(int i = 0; i < n; i++) {
cout << st << " ";
st += cd;
}
cout << β€œ\n”;
}

signed main()
{

#ifndef ONLINE_JUDGE
freopen("inputf.txt", "r" , stdin);
freopen("outputf.txt", "w" , stdout);
#endif
fast;

int t = 1;
t = readIntLn(1, MAX_T);

for(int i=1;i<=t;i++)
{
   solve();
}

assert(getchar() == -1);

cerr<<"SUCCESS\n";
cerr<<"Tests : " << t << '\n';
cerr<<"Sum of lengths : " << sum_len << '\n';
// cerr<<"Maximum length : " << max_n << '\n';
// cerr<<"Total operations : " << total_ops << '\n';
//cerr<<"Answered yes : " << yess << '\n';
//cerr<<"Answered no : " << nos << '\n';

}

Tester's Solution 2
#include <bits/stdc++.h>
using namespace std;
 
 
/*
------------------------Input Checker----------------------------------
*/
 
long long readInt(long long l,long long r,char endd){
    long long x=0;
    int cnt=0;
    int fi=-1;
    bool is_neg=false;
    while(true){
        char g=getchar();
        if(g=='-'){
            assert(fi==-1);
            is_neg=true;
            continue;
        }
        if('0'<=g && g<='9'){
            x*=10;
            x+=g-'0';
            if(cnt==0){
                fi=g-'0';
            }
            cnt++;
            assert(fi!=0 || cnt==1);
            assert(fi!=0 || is_neg==false);
 
            assert(!(cnt>19 || ( cnt==19 && fi>1) ));
        } else if(g==endd){
            if(is_neg){
                x= -x;
            }
 
            if(!(l <= x && x <= r))
            {
                cerr << l << ' ' << r << ' ' << x << '\n';
                assert(1 == 0);
            }
 
            return x;
        } else {
            assert(false);
        }
    }
}
string readString(int l,int r,char endd){
    string ret="";
    int cnt=0;
    while(true){
        char g=getchar();
        assert(g!=-1);
        if(g==endd){
            break;
        }
        cnt++;
        ret+=g;
    }
    assert(l<=cnt && cnt<=r);
    return ret;
}
long long readIntSp(long long l,long long r){
    return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
    return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
    return readString(l,r,'\n');
}
string readStringSp(int l,int r){
    return readString(l,r,' ');
}
 
 
/*
------------------------Main code starts here----------------------------------
*/
 
const int MAX_T = 1000;
const int MAX_N = 1000;
const int MAX_SUM_LEN = 5000;
 
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int sumN=0;

void solve()
{
    int n=readInt(1,MAX_N,'\n');
    sumN+=n;
    assert(sumN<=MAX_SUM_LEN);
    for(int i=1;i<=n;i++)
    {
        cout<<2*i+1<<' ';
    }
    cout<<'\n';
}

signed main()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    
    
    int t = readInt(1,MAX_T,'\n');
    
    for(int i=1;i<=t;i++)
    {    
        solve();
    }
    
    assert(getchar() == -1);
}
Editorialist's Solution
import java.util.*;
import java.io.*;
class SUBPRB{
    //SOLUTION BEGIN
    void pre() throws Exception{}
    void solve(int TC) throws Exception{
        int N = ni();
        for(int i = 1; i<= N; i++)p(2*i+" ");pn("");
    }
    //SOLUTION END
    void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
    static boolean multipleTC = true;
    FastReader in;PrintWriter out;
    void run() throws Exception{
        in = new FastReader();
        out = new PrintWriter(System.out);
        //Solution Credits: Taranpreet Singh
        int T = (multipleTC)?ni():1;
        pre();for(int t = 1; t<= T; t++)solve(t);
        out.flush();
        out.close();
    }
    public static void main(String[] args) throws Exception{
        new SUBPRB().run();
    }
    int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
    void p(Object o){out.print(o);}
    void pn(Object o){out.println(o);}
    void pni(Object o){out.println(o);out.flush();}
    String n()throws Exception{return in.next();}
    String nln()throws Exception{return in.nextLine();}
    int ni()throws Exception{return Integer.parseInt(in.next());}
    long nl()throws Exception{return Long.parseLong(in.next());}
    double nd()throws Exception{return Double.parseDouble(in.next());}

    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

        String next() throws Exception{
            while (st == null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (IOException  e){
                    throw new Exception(e.toString());
                }
            }
            return st.nextToken();
        }

        String nextLine() throws Exception{
            String str = "";
            try{   
                str = br.readLine();
            }catch (IOException e){
                throw new Exception(e.toString());
            }  
            return str;
        }
    }
}

Feel free to share your approach. Suggestions are welcomed as always. :slight_smile:

1 Like

This how I solved this problem. Printing all the n odd numbers.
Example : 1 3 5 7 9…
It satisfies all the requirements i.e the sum of any consecutive subarray is divisible by its length.

int n = 0;
cin >> n; 
int x = 0 , i = 1;
while ( x < n ) {
    cout << i << " ";
    x++;
    i += 2;
}
cout << "\n";
return;
2 Likes

hindi editorial