Bonetrousle Hackerrank Math + thinking problem

Source: Internet
Author: User

Https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle

Given a number n, and the number of K, 1--k this k, asked to select the number of B, so that the number of B and equal to N.

First of all consider the minimum value, in the 1--k select the number of the first B, is the smallest, recorded as MI. Maximum, the number of B after the addition, recorded as MX

Notice one thing: if Mi <= n <= mx. So it's absolutely doable. Because Mi always increases by 1 (while ensuring that the requirements are met), it is possible to have a solution within this range.

So first enumerate from the MI, each time the number of B to the maximum K (number of b-1 changed to k-1 ...) and so on, because the number cannot be repeated.

is always enumerated to MI >= N. Then the output scheme can be.

Bug point: After B number is too big, will explode LL. So, when the next number is added to >= N, enough, to prove that N is <= MX, there is a viable solution.

Then the simulation can be. Complexity O (b)

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 1e5 + -; LL A[MAXN];voidWork () {//Ios::sync_with_stdio (false);LL N, K, B;//scanf ("%lld%lld%lld", &n, &k, &b);CIN >> N >> k >>b; //cout << n << "<< k <<" "<< b << Endl;LL mi =0, mx =0;  for(inti =1; I <= b; ++i) {mi+=i; }     for(LL i = k; I >= K-b +1; --i) {mx+=i; //if (MX < 0) while (1);        if(MX >= N) Break;//OK    }    //cout << mi << "" << mx << endl;    if(MX <N) {printf ("-1\n"); return ; }    if(Mi >N) {printf ("-1\n"); return ; } LL Now=mi; intto =b; LLGet=K;  for(inti =1; I <= b; ++i) A[i] =i;  while(true) { now= Now-a[to] +Get; A[to]=Get; if(Now >=N) {LL cut= Now-N; A[to]-=cut;  for(inti =1; I <= B-1; ++i) {printf ("%lld", A[i]); } printf ("%lld\n", A[b]);  Break; } to--; Get--; }}intMain () {#ifdef LOCAL freopen ("Data.txt","R", stdin);#endif    intT; CIN>>T;//cout << t << Endl;     while(t--) work (); return 0;}
View Code

Bonetrousle Hackerrank Math + thinking problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.