Bonetrousle


Problem Statement :


Here's a humerus joke:

Why did Papyrus the skeleton go to the store by himself? Because he had no body to go with him!

Did you like it? Don't worry, I've got a ton more. A skele-ton.

Once upon a time, Papyrus the skeleton went to buy some pasta from the store. The store's inventory is bare-bones and they only sell one thing — boxes of uncooked spaghetti! The store always stocks exactly k boxes of pasta, and each box is numbered sequentially from 1 to k. This box number also corresponds to the number of sticks of spaghetti in the box, meaning the first box contains 1 stick, the second box contains 2 sticks, the third box contains 3 sticks, …, and the kth box contains k sticks. Because they only stock one box of each kind, the store has a tendon-cy to sell out of spaghetti.

During each trip to the store, Papyrus likes to buy exactly n sticks of spaghetti by purchasing exactly b boxes (no more, no less). Not sure which boxes to purchase, Papyrus calls Sherlock Bones for help but he's also stumped! Do you have the guts to solve this puzzle?

Given the values of n, k, and b for t trips to the store, determine which boxes Papyrus must purchase during each trip. For each trip, print a single line of b distinct space-separated integers denoting the box number for each box of spaghetti Papyrus purchases (recall that the store only has one box of each kind). If it's not possible to buy  sticks of spaghetti by purchasing  boxes, print -1 instead.

For example, Papyrus wants to purchase n =14 sticks of spaghetti in b = 3 boxes and the store has k=8 different box sizes. He can buy boxes of sizes [8,4,2], [7,5,2], [7,6,1] and other combinations. Any of the combinations will work.

Function Description

Complete the bonetrousle function in the editor below. It should return an array of integers.

bonetrousle has the following parameter(s):

n: the integer number of sticks to buy
k: the integer number of box sizes the store carries
b: the integer number of boxes to buy
Input Format

The first line contains a single integer t, the number of trips to the store.
Each of the next t lines contains three space-separated integers n, k and b, the number of sticks to buy, the number of boxes for sale and the number of boxes to buy on this trip to the store.

Constraints
1 <= t <= 20
1 <= b <= 10^5
1 <= n,k <= 10^18
b <= k



Solution :



title-img


                            Solution in C :

In C++ :





#include <bits/stdc++.h>

#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,a,b) for(int i=(a);i>=(b);i--)

#define LL long long
#define Ldouble long double
#define PI 3.1415926535897932384626

#define PII pair<int,int>
#define PLL pair<LL,LL>
#define mp make_pair
#define fi first
#define se second

using namespace std;

int t, n;
LL ttl, sto, s[100005];

void solve(){
	LL bs = 1LL * n * (n + 1) / 2, tar;
	if(ttl < bs){
		printf("-1\n");
		return;
	}
	FI(i, 1, n) s[i] = i;
	
	tar = ttl - bs;
	
	FD(i, n, 1){
		LL incre = max(0LL, min(tar, sto - (n - i) - s[i]));
		s[i] += incre;
		tar -= incre;
	}
	
	if(tar > 0){
		printf("-1\n");
		return;
	}
	
	FI(i, 1, n) printf("%lld%c", s[i], i == n ? '\n':' ');
	return;
}

int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%lld %lld %d", &ttl, &sto, &n);
		solve();
	}
	return 0;
}









In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int z = 0; z < t; z++) {
            long n = sc.nextLong();
            long k = sc.nextLong();
            int b = sc.nextInt();
            long diff = k-b;
            long[] selected = new long[b];
            long sum = 0;
            for (int i = 0; i < b; i++) {
                selected[i] = i+1;
                sum += i+1;
            }
            if (sum > n) {
                System.out.println(-1);
                continue;
            }
            long next = k+1;
            for (int i = b-1; i >= 0; i--) {
                sum += diff;
                if (sum < n) {
                    selected[i] = --next;
                } else {
                    sum -= diff;
                    selected[i] = n-sum+selected[i];
                    sum = n;
                    break;
                }
            }
            if (sum < n) {
                System.out.println(-1);
                continue;
            }
            StringBuilder print = new StringBuilder();
            for (int i = 0; i < b; i++) {
                if (i > 0)
                    print.append(" ");
                print.append(selected[i]);
            }
            System.out.println(print);
        }
    }
}








In C :





#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int t;
    long long int n, k, b;
    long long int llt, llt2, llt3;
    long long int i;
    int has_printed;
    
    scanf("%d", &t);
    
    while (t--) {
        scanf("%lld %lld %lld", &n, &k, &b);
        
        llt = (b * (b + 1)) >> 1;
        if (llt > n) {
            printf("-1\n");
            continue;
        }
        llt2 = n - llt;
        
        // divide llt2 with b
        if ((llt2 % b) == 0) {
            llt3 = llt2 / b;
            has_printed = 0;
            // if it is within limits, then we are good
            if ((b + llt3) <= k) {
                for (i = llt3 + 1; i <= llt3 + b; i++) {
                    if (has_printed) {
                        printf(" ");
                    }
                    printf("%lld", i);
                    has_printed = 1;
                }
                printf("\n");
            } else {
                printf("-1\n");
            }
        } else {
            llt3 = llt2 / b;
            // we are starting at lower end
            if ((llt3 + b) >= k) {
                printf("-1\n");
            } else {
                llt = llt2 % b;
                has_printed = 0;
                for (i = llt3 + 1; i <= (llt3 + b - llt); i++) {
                    if (has_printed) {
                        printf(" ");
                    }
                    printf("%lld", i);
                    has_printed = 1;
                }
                for (; i <= (llt3 + b); i++) {
                    printf(" ");
                    printf("%lld", i + 1);
                }
                printf("\n");
            }
        }
    }
    
    return 0;
}








In Python3 :





def sum(l, r):
    return r * (r + 1) // 2 - l * (l - 1) // 2

t = int(input())
while t > 0:
    t -= 1
    
    n, k, b = map(int, input().split())

    if sum(1, b) > n or sum(k - b + 1, k) < n: print(-1)
    else:
        a = []
        while n > 0:
            x = min(k, n - sum(1, b - 1))
            a += [x]

            k = x - 1
            b -= 1
            n -= x
        
        print(' '.join(map(str, a)))
                        








View More Similar Problems

The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →

Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →