ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1209. 1, 10, 100, 1000...

1209. 1, 10, 100, 1000... - WA on #5
Posted by binarysakir 30 Aug 2016 20:42
If the input is P then the output will be 1 if P can be expressed as 1+(n(n-1)/2)

=> 1+(n(n-1)/2) = p
=> 2+n(n-1) = 2p
=> n(n-1) = 2p-2

To find such n I used floor and ceil of sqrt(2p-2)

What's wrong with my logic?

#include <bits/stdc++.h>

using namespace std;

int main(){
    double n, k, a, b, root;
    vector<int> v;
    cin >> k;
    while(k--){
        cin >> n;
        root = sqrt((2*n) - 2);
        a = ceil(root);
        b = floor(root);
        if(a == root && b == root){
            v.push_back(0);
        }else{
            if(a * b == (2*n) - 2) v.push_back(1);
            else v.push_back(0);
        }
    }
    for (int i = 0; i < v.size(); ++i){
        if(i == v.size() - 1) cout << v[i];
        else cout << v[i] << " ";
    }
    return 0;
}

Edited by author 30.08.2016 20:42