233B - Non-square Equation - CodeForces Solution


binary search brute force math *1400

Please click on ads to support us..

Python Code:

import collections
from decimal import Decimal
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os
from unicodedata import decimal
def valid(i,j,n,m):
        if i<n and i>=0 and j>=0 and j< m :return True         return  False
def sumn(i,n):
    return (n-i)*(i+n)/2
def sqfun(a,b,c):
    return (-b+math.sqrt(b*b-4*a*c))/2*a
def getprime(num):
        if  all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):return  True



def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return [int(i) for i in input().split()]
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]



def solve():
    n=inp()
    for sm in range(1,91):
                sx = (Decimal((sm**2)+4*n).sqrt()-sm)/2
        if sx>0 and int(sx)==sx:
            if sm==sum([int(i) for i in str(int(sx))]):
                return int(sx)
    return -1
if __name__ == "__main__":
        print(solve())








C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define AYAALI ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define FOR(i, a, b) for(int i = int(a); i < int(b); i++)
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define ll long long
#define pb(x) push_back(x)
#define MP(x, y) make_pair(x,y)
#define fi first
#define se second
#define endl '\n'
#define all(v) v.begin(), v.end()
const double EPS = 1e-9;
const ll N=1e5+1;
//---------------------------------------------
/*void files(){
    freopen("pails.in", "r", stdin);
    freopen("pails.out", "w", stdout);
}*/

ll s(ll i){
    ll sum=0;
    // 99
    while(i>0){
        sum+=(i%10);
        i/=10;
    }
    return sum;
}
void solve()
{
    ll n;
    cin>>n;
    ll ans=0;
    // x biggest val 10^9 biggest sum 999999999
    // 1.53 - 1 = 0.53 *10 = 5.3
    for(ll i =1;i<=100;i++){
        ll in = (-i+sqrt(pow(i,2)+(4*n)))/2;
        double de = (-i+sqrt(pow(i,2)+(4*n)))/2;
        if(in>0 && (de-in)*1000000000==0 ){
             if(i == s((-i+sqrt(pow(i,2)+(4*n)))/2)){ans=(-i+sqrt(pow(i,2)+(4*n)))/2;break;}
        }
    }
    if(ans)cout<<ans;
    else cout<<-1;





}


int main() {
    AYAALI
    ll t=1;
    //files();
    //cin>>t;
    while(t--){
        solve();}
}


Comments

Submit
0 Comments
More Questions

MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image