Skip to content

Instantly share code, notes, and snippets.

@sudoankit
Created April 17, 2020 17:20
Show Gist options
  • Save sudoankit/7a5dfa8dd0e0ac46e481648ff630b6f7 to your computer and use it in GitHub Desktop.
Save sudoankit/7a5dfa8dd0e0ac46e481648ff630b6f7 to your computer and use it in GitHub Desktop.
Lazy caterer's sequence, Timus Online Judge: 1209 - 1, 10, 100, 1000...
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
long n;
cin >> n;
while(n--){
long double p, test;
cin >> p;
test = sqrt(8*p - 7);
if (test - floor(test) == 0)
cout << 1 << ' ';
else
cout << 0 << ' ';
}
}
// This actually is disguised as a problem which uses the Lazy caterer's sequence --
// central polygonal numbers, describes the maximum number of pieces of a disk
// (a pancake or pizza is usually used to describe the situation) that can be made
// with a given number of straight cuts. https://en.wikipedia.org/wiki/Lazy_caterer%27s_sequence
// 1 + n(n+1)/2 = input (p)
// solving if sqrt(8p-7) is perfect it's true, else false.
// AC in 1 try.
@SymphonySimper
Copy link

SymphonySimper commented Oct 16, 2023

Why - 7?

@sudoankit
Copy link
Author

Solve for $n$ in $n^2+n + 2(1- p) = 0$ using quadratic formula ( $-b \pm \sqrt{b^2-4ac} / 2a$ )

@SymphonySimper
Copy link

Got it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment