1294C - Product of Three Numbers - CodeForces Solution


greedy math number theory *1300

Please click on ads to support us..

Python Code:

import os
import sys
from io import BytesIO, IOBase

_str = str
str = lambda x=b"": x if type(x) is bytes else _str(x).encode()

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

import math
def ps(n) :
    i = 2
    while i <= math.sqrt(n):
        if (n % i == 0) :
            if (n / i == i) :
                arr.add(i)
            else :
                arr.add(i)
                arr.add(n//i)
        i = i + 1  
        
def ps2(n) :
    i = 2
    while i <= math.sqrt(n):
        if (n % i == 0) :
            if (n / i == i) :
                arr.append(i)
            else :
                arr.append(i)
                arr.append(n//i)
            break
        i = i + 1  

import math
for _ in range(int(input())):
    n = int(input())
    arr = []
    ps2(n)
        if len(arr) == 0:
        print("NO")
    else:
        z = arr[0]
        ans = False
        zz = n//arr[0]
        arr = set()
        ps(zz)
        for i in arr:
            if i == z:
                continue
            if zz//i == i or zz//i == z:
                continue
            aa = i
            bb = zz//i
            ans = True
            break
        if ans:
            print("YES")
            print(z, aa, bb)
        else:
            print("NO")

C++ Code:

#include <bits/stdc++.h>
using namespace std; 
#define endl                   "\n" ;
#define int                    long long
#define rep(i,a,b)             for(int i = a ; i <= b ; i++)
#define rev(i,a,b)             for (int i = a ; i >= b ; i--)
#define vi                     vector<int>
#define pii                    pair<int,int>
#define pb                     push_back
#define ppb                    pop_back
#define pf                     push_front
#define ppf                    pop_front 
#define all(x)                 (x).begin(),(x).end()
#define setit(a,b)             memset(a,b,sizeof(a))
#define mp                     make_pair
#define ff                     first 
#define ss                     second
#define least                  INT_MIN
#define great                  INT_MAX       
#define its(n)                 int n ; cin>>n 
#define get(arr,n)             int arr[n] ; rep(i,0,n-1){cin>>arr[i] ;}
#define give(x)                cout<<#x<<" is "<<x<<endl;
#define out(x)                 cout<<x<<endl;
#define yes                    cout<<"YES"<<endl;
#define no                     cout<<"NO"<<endl;
#define c(x)                   cout<<((x)? "YES" : "NO")<<endl ;
#define maxe                   *max_element 
#define mine                   *min_element

const int mod = 998244353;
const int inf = 1e18 ;
const int Mod = 1000000007 ;
/*--------------------------FUNCTIONS----------------------------------*/
int fact(int x ){
    int fact[x+1] ;
    fact[0] = 1 ;
    rep(i,1,x){
        fact[i] = (fact[i-1] * i) ;
    }
    return fact[x] ;
}


bool prime(int x){
    int i = 2 ;
    while(i<=sqrt(x)){
        if(x%i == 0){return false ;}
        i++ ;
    }
        return true ;
}


bool checksieve(int n)
{
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
 
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    return prime[n] ;
}


int binexp(int a , int b , int m){
    int ans = 1 ;
    while(b > 0){
        if(b&1){
            ans = (ans*a)%m ;
        }
        a = (a*a)%m ;
        b>>=1 ;
    }
    return ans ;
}


int modinv(int a , int m ){
    return binexp(a , m-2 , m);
}


int nCr(int n , int r){
   return fact(n)/(fact(r)*fact(n-r)) ;
}
/*--------------------------MAIN CODE----------------------------------*/
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL) ;

    int t ;
    cin>>t ;
    while(t--){ 
        its(n) ;
        int i = 2 ;
        int a = 0 , b = 0 , c = 0 ;
        while( i <= sqrt(n)){
            if(n % i == 0){
                a = i ;
                n /= i ;
                break ;
            }
            i++ ;
        }
        int temp = a + 1;
        while(temp <= sqrt(n)){
            if(n % temp == 0 && temp != n/temp){
                b = temp ; c = n/temp ; break ;
            }
            temp++ ;
        }
        if(a == 0 || b == 0 || c == 0){no}
        else{
            yes ;
            cout<<a<<" "<<b<<" "<<c<<endl;
        }
    }
 return 0 ;
}


Comments

Submit
0 Comments
More Questions

1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles