Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t=int(input()) def printDivisors(n,a) : i=2 f=0 while(i*i<=n): if n%i==0: j=i+1 n=n//i while(j*j<=n): if n%j==0: if j!=n//j: print('YES') print(i,j,n//j) f=1 break j+=1 if f==1: break i+=1 if f==0: print('NO') for _ in range(t): a=[] n=int(input()) printDivisors(n,a)
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
def main(): def divisors(n): div = [] for i in range(2, int(n**0.5)): #n//2 + 1):#int(n ** 0.5)): if n % i == 0: div.append(i) n //= i if len(div) == 2: if n not in div and n != 1: div.append(n) break return div for _ in range(int(input())): n = int(input()) d = divisors(n) #print(d) if len(d) < 3: print('NO') continue print('YES') print(*d) ''' a = d[0] n = n // a b = d[1] c = n // b if c != 1 and c != b and c != a: print('YES') print(a, b, c) else: print('NO')''' main() ''' i = 2 while i < n and len(div) < 2: if n % i == 0: div.append(i) #t = n // i n //= i i += 1 if n not in div: div.append(n) return div'''
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.util.*; public class Main{ public static void main(String args[]){ Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); int count=0; int a=0,b=0; for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0){ n=n/i; if(a==0){ count++; a=i; }else if(b==0 && i!=n){ b=i; count++; break; } } } if(count==2){ System.out.println("YES"); System.out.println(a+" "+b+" "+n); }else{ System.out.println("NO"); } } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math T = int(input()) def check(n,p): ans = -1 for i in range(p+1, int(math.sqrt(n)) + 1): if n % i == 0: ans = i break return ans for i in range(T): l = [] n = int(input()) ans = 'YES' hello = 1 for i in range(3): hello = check(n,hello) if hello == -1 and n < 2: ans = 'NO' break else: if hello == -1 or i==2: hello = n if len(l) > 0 and l[-1] == hello: ans = 'NO' break n = n // hello l.append(hello) print(ans) if ans == 'YES': print(str(l[0]) + ' ' + str(l[1]) + ' ' + str(l[2]))
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t=int(input()) for i in range(t): n=int(input()) a=0 b=0 c=0 for i in range(2,int(n**0.5)+2): if n%i==0: n//=i a=i break if a==0: print("NO") continue else: for j in range(a+1,int(n**0.5)+2): if n%j==0 and n//j!=j and n//j!=a: b=j c=n//j break if b==0: print("NO") continue else : print("YES") print(a,b,c)
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math t = int(input()) while t>0: t-=1 n = int(input()) sn = n l = [] for i in range(2,int(math.sqrt(n))+1): if n%i == 0: l.append(i) n = n//i c = i break else: print("NO") continue for i in range(c+1,int(math.sqrt(n))+1): if n%i == 0: l.append(i) n = n//i break else: print("NO") continue if (sn//(l[0]*l[1]))!=1 and (sn//(l[0]*l[1]) not in l): l.append(sn//(l[0]*l[1])) print("YES") print(*l) else: print("NO")
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import sys def fastio(): from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() def debug(*var, sep = ' ', end = '\n'): print(*var, file=sys.stderr, end = end, sep = sep) INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br def ok(n): # for i in range() pass def fac(n): res = [] for i in range(2, int(n ** 0.5 + 1)): if n % i: continue res.append(i) if i ** 2 != n: res.append(n // i) return sorted(res) t, = I() while t: t -= 1 n, = I() ans = 'YES' f = fac(n) if len(f) < 3: print('NO') continue m = len(f) for i in range(m): for j in range(i + 1, m): k = n // (f[i] * f[j]) if k in f and k != f[i] and k != f[j]: print('YES') print(f[i], f[j], k) break else: continue break else: print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math def fact(n): ans = 1 for i in range(2, n+1): ans*= i return ans def comb(n, c): return fact(n)//(fact(n-c)*c) for _ in range(int(input())): n= int(input()) ans = [] for i in range(2, int(math.sqrt(n))+1): if(n%i==0 and i not in ans): n//=i ans.append(i) break for i in range(2,int(math.sqrt(n))+1): if(n%i==0 and i not in ans): n//=i ans.append(i) break if(len(ans)<2 or n==1 or n in ans): print('NO') else: ans.append(n) print('YES') print(ans[0], ans[1], ans[2])
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class Solution{ public static void main(String[] args){ try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); for(int i=0;i<t;i++){ int n=Integer.parseInt(br.readLine()); int u=0,v=0; for(int j=2;j<Math.sqrt(n);j++){ if(n%j==0){ u=j; n=n/j; break; } } if(u!=0){ for(int j=2;j<Math.sqrt(n);j++){ if(n%j==0 && j!=u && n/j != u){ v=j; n=n/j; break; } } } if(v!=0){ System.out.println("YES"); System.out.println(u+" "+v+" "+n); }else{ System.out.println("NO"); } } }catch(Exception e){ System.out.println("kkkk"); } } static int lcm(int a,int b){ return (a*b)/hcf(a,b); } static int hcf(int a,int b){ if(a==0){ return b; } if(b==0){ return a; } if(a>b) return hcf(a%b,b); return hcf(a,b%a); } static class pair{ int a,b; public pair(int a,int b){ this.a=a; this.b=b; } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.util.ArrayList; import java.util.BitSet; import java.util.Scanner; public class CF1294C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); BitSet sieve = new BitSet(); for(int i=2; i<=1000000; i++) sieve.set(i); for(int i=2; i<=1000000; i++) if(sieve.get(i)) for(int j=i+i; j<=1000000; j+=i) sieve.set(j, false); ArrayList<Integer> primes = new ArrayList<>(); for(int i=2; i<=1000000; i++) if(sieve.get(i)) primes.add(i); while(tc-->0){ int n = sc.nextInt(); int sqrt = (int) Math.sqrt(n)+1; ArrayList<Integer> ans = new ArrayList<>(); for(int i=0; ans.size()<3 && i<primes.size() && primes.get(i)<sqrt; i++){ int prime = primes.get(i); if(n%prime==0){ n/=prime; ans.add(prime); if(ans.size()==2 && !ans.contains(n) && n>1) ans.add(n); if(ans.size()<3 && n%(prime*prime)==0){ n/=prime*prime; ans.add(prime*prime); if(ans.size()==2 && !ans.contains(n) && n>1) ans.add(n); } } } System.out.println(ans.size()!=3 ? "NO" : "YES\n"+ans.get(0)+" "+ans.get(1)+" "+ans.get(2)); } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
def getprod(n): res = [] i = 1 while(i*i <= n): if(n%i == 0): res.append(i) if(n//i != i): res.append(n//i) i += 1 return res for _ in range(int(input())): num = int(input()) res = getprod(num) if(len(res) <= 2): print('NO') else: res.sort() a,b,c = -1,-1,-1 i = 0 n = len(res) flag = 0 while(i < n): j,k = i+1,n-1 while(j < k): x = res[i]*res[j]*res[k] if(x == num and res[i]>= 2 and res[j]>= 2 and res[k]>= 2): a,b,c = res[i],res[j],res[k] flag = 1 break elif(x < num): j += 1 else: k -= 1 if(flag == 1): break i += 1 if(a == -1): print('NO') else: print('YES') print(a,b,c)
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package ieee; import java.util.Scanner; import java.util.ArrayList; /** * * @author LAPTOP */ public class IEEE { /** * @param args the command line arguments */ public static boolean isprime (long n ) { if (n==2) return true; if(n<2 || n%2==0) return false ; for (long i = 3 ; i*i<=n ; i+=2) if(n%i==0) return false; return true; } public static void main(String[] args) { Scanner sc = new Scanner (System.in); int t = sc.nextInt(); while(t>0) { t--; int n = sc.nextInt(); ArrayList <Integer> arr = new ArrayList<Integer>(); for (int i = 2 ; i*i < n ; i++) { if (n%i==0) { arr.add(i); n/=i; } if (arr.size()==2) { arr.add(n); break; } } if (arr.size()<3 ) System.out.println("NO"); else { System.out.println("YES"); for (int i = 0; i < 3; i++) { System.out.print(arr.get(i) +" "); } System.out.println(""); } } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.util.*; import java.io.*; public class Product_of_Three_Numbers { static int mod = (int) (1e9 + 7); public static void main(String[] args) throws java.lang.Exception { /* Let a be the minimum divisor of n greater than 1. Then let b be the minimum divisor of na that isn't equal a and 1. If n/ab isn't equal a, b and 1 then the answer is "YES", otherwise the answer is "NO". Time complexity: O(√n) per query. */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); Set<Integer> h=new HashSet<>(); for(int i=2; i*i<=n; i++) { if(n%i==0 && !h.contains(i)) { h.add(i); n/=i; break; } } for(int i=2; i*i<=n; i++) { if(n%i==0 && !h.contains(i)) { h.add(i); n/=i; break; } } if(h.size()<2 ||h.contains(n)|| n==1) System.out.println("NO"); else { System.out.println("YES"); h.add(n); for(int x: h) System.out.print(x+" "); System.out.println(); } } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import sys I=sys.stdin.readline ans="" for _ in range(int(I())): n=int(input()) fac=[(i,n//i) for i in range(2,int(n**.5)+1) if n%i==0] #print(fac) if len(fac)!=0: x=fac[0][1] flag=1 for i in range(2,int(x**.5)+1): if x%i==0 and i!=fac[0][0]: if i!=x//i: ans+="YES\n" ans+="{} {} {}\n".format(fac[0][0],i,x//i) flag=0 break if flag: ans+="NO\n" else: ans+="NO\n" print(ans)
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
"""T=int(input()) for _ in range(0,T): n=int(input()) a,b=map(int,input().split()) s=input() s=[int(x) for x in input().split()] for i in range(0,len(s)): a,b=map(int,input().split())""" import math T=int(input()) for _ in range(0,T): N=int(input()) n=N L=[] while (n % 2 == 0): L.append(2) n = n // 2 for i in range(3,int(math.sqrt(n))+1,2): while (n % i== 0): L.append(i) n = n // i if (n > 2): L.append(n) if(len(L)<3): print('NO') else: t1=L[0] t2=L[1] t3=1 if(L[1]==L[0]): t2=L[1]*L[2] for j in range(3,len(L)): t3*=L[j] else: for j in range(2,len(L)): t3*=L[j] if(t1!=t2 and t2!=t3 and t1!=t3 and t1>1 and t2>1 and t3>1): print('YES') print(t1,t2,t3) else: print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math t=int(input()) for q in range(t): n=int(input()) m=n fact=[] while m%2==0: fact.append(2) m = m / 2 for i in range(3,int(math.sqrt(m))+1,2): while m % i== 0: fact.append(i) m = m / i if m > 2: fact.append(m) dist = list(set(fact)) if len(dist)==1: if fact.count(dist[0])>4: o=dist[0] t=dist[0]*dist[0] if (n/(o*t))>2 and int((n/(o*t)))!=o and int((n/(o*t)))!=t: print('YES') print(int(o),int(t),int((n/(o*t)))) else: print('NO') else: print('NO') elif len(dist)==2: o=dist[0] t=dist[1] if (fact.count(o)+fact.count(t))>=4: if (n/(o*t))>2 and int((n/(o*t)))!=o and int((n/(o*t)))!=t: print('YES') print(int(o),int(t),int((n/(o*t)))) else: print('NO') else: print('NO') if len(dist)>2: o=dist[0] t=dist[1] if (n/(o*t))>2 and int((n/(o*t)))!=o and int((n/(o*t)))!=t: print('YES') print(int(o),int(t),int((n//(o*t)))) else: print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { int n=ni(); if(n==2){ pn("NO"); return; } ArrayList<Pair> li = new ArrayList<>(); int t=n; long po=0l; int c=0; while (n % 2 == 0) { //System.out.print(2 + " "); c++; n /= 2; } if(c!=0){ li.add(new Pair(2,c)); po+=c; } for (int i = 3; i <= Math.sqrt(n); i += 2) { c=0; while (n % i == 0) { // System.out.print(i + " "); c++; n /= i; } if(c!=0){ li.add(new Pair(i,c)); po+=c; } } if (n > 2) { if(n==t){ pn("NO"); return; } li.add(new Pair(n,1)); } int a=li.get(0).n,b=1; li.get(0).p=li.get(0).p-1; // System.out.println("pow"+li.get(0).p); if(li.get(0).p>=2) b=(li.get(0).n)*(li.get(0).n); else if(li.get(0).p==1){ if(li.size()==1){ pn("NO"); return; } b=(li.get(0).n)*(li.get(1).n); } else{ if(li.size()==1){ pn("NO"); return; } b=li.get(1).n; } long ca=t/(a*b); if(ca==a || ca==b || ca==1){ pn("NO"); return; } pn("YES"); pn(a+" "+b+" "+ca); } static class Pair{int n,p; public Pair(int n,int p){this.n=n;this.p=p;}} static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc=new AnotherReader(); boolean oj = true; oj = System.getProperty("ONLINE_JUDGE") != null; if(!oj) sc=new AnotherReader(100); long s = System.currentTimeMillis(); int t=ni(); while(t-->0) process(); out.flush(); if(!oj) System.out.println(System.currentTimeMillis()-s+"ms"); System.out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);System.out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static boolean multipleTC=false; ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n; cin >> n; set<int> used; for (int i = 2; i * i <= n; i++) { if (n % i == 0 && !used.count(i)) { used.insert(i); n /= i; break; } } for (int i = 2; i * i <= n; i++) { if (n % i == 0 && !used.count(i)) { used.insert(i); n /= i; break; } } if (used.size() < 2 || used.count(n) || n == 1) { cout << "NO" << endl; } else { cout << "YES" << "\n"; used.insert(n); for (auto it : used) cout << it << " "; cout << endl; } } }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; void gao(int n) { for (int i = 2; i * i < n; i++) { if (n % i == 0) { for (int j = i + 1; j * j < n / i; j++) { if (n / i % j == 0) { cout << "YES" << endl; cout << i << ' ' << j << ' ' << n / i / j << endl; return; } } } } cout << "NO" << endl; return; } int main() { int t, n; cin >> t; for (int tt = 0; tt < t; tt++) { cin >> n; gao(n); } return 0; }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastReader scan = new FastReader(); //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("marathon.out"))); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)),true); Task solver = new Task(); int t = scan.nextInt(); //int t = 1; for(int i = 1; i <= t; i++) solver.solve(i, scan, out); out.close(); } static class Task { public void solve(int testNumber, FastReader sc, PrintWriter pw) { int q = sc.nextInt(); int n = 0; int m = 0; boolean a = false; boolean b = false; for(n=2;n<=Math.sqrt(q);n++){ if(q%n==0){ a=true; m=q/n; break; } } int o=n+1; for(;o<=Math.sqrt(m);o++){ if(m%o==0){ b=true; m/=o; break; } } if(n==o||!a||!b||o==m||n==m){ pw.println("NO"); } else{ pw.println("YES"); pw.println(n+" "+m+" "+o); } } } static class tup implements Comparable<tup>{ int a, b; tup(int a, int b){ this.a=a; this.b=b; } @Override public int compareTo(tup o) { return Integer.compare(Math.abs(b),Math.abs(o.b)); } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import sys import math input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().split()) inl = lambda: list(map(int, input().split())) t = ini() ans = [] andsindex = [0] * t for _ in range(t): n = ini() i = 2 tmp = [] while n >= i * i: if i == 5 and n % 5 == 0 and 4 in tmp: tmp.remove(4) if n % i == 0: tmp.append(i) if len(tmp) == 3: break i += 1 if len(tmp) <= 1: ans.append("NO") elif len(tmp) >= 2: if len(tmp) == 3: y = (n / (tmp[0] * tmp[2])).is_integer() if tmp[0] * tmp[1] * tmp[2] == n: ans.append("YES") andsindex[len(ans) - 1] = f"{tmp[0]} {tmp[1]} {tmp[2]}" # print(tmp[0], tmp[1], tmp[2]) continue elif y and n / (tmp[0] * tmp[2]) not in tmp: ans.append("YES") andsindex[len(ans) - 1] = f"{tmp[0]} {tmp[2]} {n // (tmp[0] * tmp[2])}" # print(tmp[0], tmp[2], n // (tmp[0] * tmp[2])) continue x = (n / (tmp[0] * tmp[1])).is_integer() if x and n / (tmp[0] * tmp[1]) not in tmp: ans.append("YES") andsindex[len(ans) - 1] = f"{tmp[0]} {tmp[1]} {n // (tmp[0] * tmp[1])}" # print(tmp[0], tmp[1], n // (tmp[0] * tmp[1])) else: ans.append("NO") for i in range(t): print(ans[i]) if ans[i] == "YES": print(andsindex[i])
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; void solve() { int n, i, cnt = 0, d, ar[3]; cin >> n; for (i = 2; i * i <= n; i++) if (n % i == 0) { ar[cnt++] = i; n /= i; if (cnt == 2) break; } if (cnt == 2) { ar[2] = n; sort(ar, ar + 3); if (ar[0] != ar[1] && ar[1] != ar[2]) { cout << "YES\n" << ar[0] << ' ' << ar[1] << ' ' << ar[2] << '\n'; return; } } cout << "NO\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) solve(); return 0; }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
I = input pr = print def main(): for _ in range(int(I())): ar=[] n,i=int(I()),2 while i*i<n and len(ar)<2: if n%i==0:ar.append(i);n//=i i+=1 if len(ar)==2:pr('YES');pr(*ar,n) else:pr('NO') main()
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
from sys import stdin, stdout from math import sqrt def solve(): n = int(input()) st = set() for i in range(2, int(sqrt(n)) + 1): if n%i == 0 and i not in st: st.add(i) n /= i break for i in range(2, int(sqrt(n)) + 1): if n%i == 0 and i not in st: st.add(i) n /= i break if n == 1 or n in st or len(st) < 2: print("NO") return 0 else: print("YES") A = [] for i in st: A.append(int(i)) A.append(int(n)) print(A[0], A[1], A[2]) return 0 t = int(input()) for i in range(t): solve()
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.io.*; import java.lang.*; import java.math.BigInteger; import java.util.*; public class Main{ static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { int q=scanner.nextInt(); while(q-->0) { int n=scanner.nextInt(); Set<Integer>set=new HashSet<Integer>(); for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0) { set.add(i); n/=i; if(set.size()==2) { set.add(n); break; } } } if(set.size()==3) { System.out.println("YES"); for(int i:set) { System.out.print(i+" "); } System.out.println(); }else System.out.println("No"); } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
from math import sqrt def fact(k): for i in range(2,(int(sqrt(k)+1))): if k%i == 0: if k//i != i: f = k//i return f,i return 0,0 def main(): t = int(input()) for __ in range(t): n = int(input()) flag = False for i in range(2,(int(sqrt(n)+1))): if n%i == 0: if n//i != i: f = n//i a,b = fact(i) c,d = fact(f) if a != f and b != f and(a != 0): print("YES") print(a,b,f) flag = True break elif c != i and d != i and(d != 0): print("YES") print(c,d,i) flag = True break if not flag: print("NO") if __name__ == '__main__': main()
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; vector<int> adj[200001]; long long int vist[200001], maxx; void dfs(long long int node) { vist[node] = 1; maxx = max(maxx, node); for (long long int child : adj[node]) if (!vist[child]) dfs(child); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { int n; cin >> n; int x = sqrt(n); bool ok = false; int i, j, k; for (i = 2; i <= x; i++) { int n1 = n; if (n1 % i == 0) { n1 /= i; for (j = i + 1; j <= x; j++) { if (n1 % j == 0 && j != i) { k = n1 /= j; if (k != i && k != j) { ok = true; break; } } } if (ok) break; } } if (ok) cout << "YES\n" << i << " " << j << " " << k << endl; else cout << "NO\n"; } return 0; }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; void printDivisors(long long n, set<long long> &s, vector<long long> &v) { for (long long i = 2; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) { s.insert(i); v.push_back(i); } else { s.insert(i); s.insert(n / i); v.push_back(i); v.push_back(n / i); } } } } int main() { int t; cin >> t; while (t--) { long long n; cin >> n; set<long long> s; bool flag = 0; vector<long long> v; printDivisors(n, s, v); for (int i = 0; i < v.size(); i++) { for (int j = i + 1; j < v.size(); j++) { if (n % (v[i] * v[j]) == 0 && s.find(n / (v[i] * v[j])) != s.end() && v[i] != v[j] && v[i] != n / (v[i] * v[j]) && v[j] != n / (v[i] * v[j])) { cout << "YES" << '\n'; cout << v[i] << " " << v[j] << " " << n / (v[i] * v[j]) << '\n'; flag = 1; break; } } if (flag == 1) break; } if (flag == 0) cout << "NO" << '\n'; } return 0; }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math t = int(input()) for i in range(t): a = 0 b = 0 n = int(input()) for j in range (int(math.sqrt(n))-1): if n%(j+2) == 0: n = n//(j+2) a = j+2 break if a == 0: print("NO") elif a != 0: for j in range (int(math.sqrt(n))-2): if n%(j+3) == 0 and j+3 != a: n = n//(j+3) if n != j+3: b = j+3 break if b == 0: print("NO") else: print("YES") print(a, b, n)
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math t = int(input()) for xx in range(t): n = int(input()) i = 2 fl = 0 divs = [] while(i*i <= n): if n%i == 0: divs.append(i) n//=i if len(divs) == 2: break i += 1 if len(divs) < 2: fl = 1 if fl == 1: print("NO") continue kr = n if kr == 1 or kr == divs[0] or kr == divs[1]: print("NO") continue else: divs.append(kr) print("YES") print(" ".join([str(v) for v in divs]))
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
a, i = int(input()), 0 def pro(a): p, i = 0, 2 while i * i <= a: if a % i == 0: p = i break i = i + 1 return p def f1(k, ak, a): i, t = 2, 0 while i * i <= ak: if a % i == 0: if ak // i != i and i != k and (ak // i) * i * k == a: print("YES") print(int(ak / i), int(i), int(k)) t = 1 break i = i + 1 if t == 0: print("NO") return 0 def ff(a): k = pro(a) if k == 0: print("NO") else: f1(k, a / k, a) return 0 while i != a: x = int(input()) ff(x) i = i + 1
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
for _ in range(int(input())): x = int(input()) li = [] for i in range(2,int(x**0.5) +1): if x%i==0: li.append(i) x = x//i break for i in range(2,int(x**0.5) +1): if x%i==0 and i not in li: li.append(i) x = x//i break if len(li) <2 or x==1 or x in li: print('NO') else: print('YES') print(*li, x)
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author GYSHGX868 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) { solver.solve(i, in, out); } out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] result = new int[3]; int count = 0; for (int i = 2; i * i < n; i++) { if (n % i == 0) { result[count++] = i; n /= i; if (count == 2) { result[count++] = n; break; } } } if (count != 3) { out.printLine("NO"); } else if (result[0] == result[1] || result[1] == result[2] || result[0] == result[2]) { out.printLine("NO"); } else { out.printLine("YES"); out.printLine(result); } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.io.*; import java.util.*; public class ProductofThreeNumbers { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int a,b,c; a=b=c=-1; for(int i=2;i*i<n;i++) { if(n%i==0) { a = i; break; } } if(a==-1) out.println("NO"); else { n /= a; for(int i=a+1;i*i<n;i++) { if(n%i==0&&n/i!=i) { b = i; break; } } if(b==-1) out.println("NO"); else { n /= b; c = n; if(c<2||c==a) out.println("NO"); else { out.println("YES"); out.println(a+" "+b+" "+c); } } } }out.close(); } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools from collections import deque,defaultdict,OrderedDict import collections def primeFactors(n): pf=[] # Print the number of two's that divide n while n % 2 == 0: pf.append(2) n = n / 2 # n must be odd at this point # so a skip of 2 ( i = i + 2) can be used for i in range(3,int(math.sqrt(n))+1,2): # while i divides n , print i ad divide n while n % i== 0: pf.append(int(i)) n = n /i # Condition if n is a prime # number greater than 2 if n > 2: pf.append(int(n)) return pf def main(): starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") #Solving Area Starts--> for _ in range(ri()): n=ri() a=primeFactors(n) amult=1 for i in range(1,len(a)-1): amult=amult*a[i] # print(a) t=0 if len(a)<3: print("NO") else: d={} for i in a: if i in d: d[i]+=1 else: d[i]=1 b=list(set(a)) z=len(b) if z>=3: print("YES") ans=[a[0],a[-1],amult] t=1 if z==2: c=0 for i in d: if d[i]>=3: print("YES") ans=[a[0],a[-1],amult] t=1 break if d[i]>=2: c+=1 if c>=2: print("YES") ans=[a[0],a[-1],amult] t=1 if t==0: print("NO") if z==1: t=0 for i in d: if d[i]>=6: print("YES") ans=[a[0],a[0]*2,n//(a[0]**3)] t=1 break if t==0: print("NO") if t==1: print(*ans) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) 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, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) 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() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main()
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
def factor(n): dict1={} for i in range(2,int(pow(n,0.5))+1): if n%i==0: if i not in dict1: dict1[i]=1 if n//i not in dict1: dict1[n//i]=1 return dict1 for t in range(int(input())): n=int(input()) dict1=factor(n) dict1=sorted(dict1) #print(dict1) a=0 b=0 c=-1 for i in dict1: if a==0: a=i else: b=i d=n//(a*b) if d!=a and d!=b and d in dict1: c=d break if c==-1: print("NO") else: print("YES") print(a,b,c,sep=" ")
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; int n; int num[maxn], cnt; int seq[maxn]; bool Find(int res, int now) { for (int i = 1; i <= now; i++) { if (seq[i] == res) return true; } return false; } void Init(); int main() { int T; scanf("%d", &T); while (T--) Init(); return 0; } void Init() { scanf("%d", &n); int cun = n; cnt = 0; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) { n /= i; num[++cnt] = i; } } } if (n > 1) num[++cnt] = n; int now = 0, res = 1, i; for (i = 1; i <= cnt; i++) { res *= num[i]; if (!Find(res, now)) { seq[++now] = res; seq[now] = res; if (now == 3) { seq[now] = cun / seq[1] / seq[2]; break; } res = 1; } } if (now == 3) { puts("YES"); for (int i = 1; i <= 3; i++) printf("%d ", seq[i]); puts(""); } else puts("NO"); }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> v; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { v.push_back(i); if (v.size() == 2) { v.push_back(n / i); break; } n /= i; } } v.erase(unique(v.begin(), v.end()), v.end()); if (v.size() != 3) { cout << "NO" << "\n"; } else { cout << "YES" << "\n"; for (const auto &e : v) { cout << e << " "; } cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { solve(); } return 0; }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t=int(input()) for i in range(t): n=int(input()) l=[0, 0, 0] tmp=0 m=n i=2 while i**2<=m and tmp<2: if n%i==0: n//=i l[tmp]=i tmp+=1 i+=1 if tmp==2 and n>l[1]: print("YES") l[2]=n print(*l) else: print("NO")
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.util.Scanner; public class Product_Of_Three_Numbers { public static void main(String args[]) { Scanner sc = new Scanner(System.in); //System.out.println("Enter the number of test cases :"); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); find(n); } } public static void find(int n) { int k = 0; int m=n; int res[] = new int[3]; for(int i=2;i*i<=n;i++) { if(k==1 && n%i==0) { res[k] = i; k++; break; } if(k<1 && n%i==0) { res[k]=i; n=n/i; k++; } } int r=0; if(res[0]!=0 && res[1]!=0) { r=m/(res[0]*res[1]); if(r !=res[0] && r!=res[1] && r!=1) { System.out.println("YES"); System.out.println(res[0]+" "+res[1]+" "+r); } else System.out.println("NO"); } else System.out.println("NO"); } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
for t in range(int(input())): n=int(input()) flag=0 for i in range(2,10001): if(n%i==0): flag=1 break if(flag==1): a=i n=n//i for i in range(2,100001): if(n%i==0 and i!=a): flag=2 break b=i c=n//i if(flag==2 and b!=c and c!=1 and c!=a): print("YES") print(a,b,c) else: print("NO")
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math def printDivisors(n) : i = 2 c = [] while i <= math.sqrt(n): if (n % i == 0) : if (n / i == i) : c.append(n//i) else : c.extend([i,n//i]) i = i + 1 return c def countTriplets(arr, n, m): count = 0 arr.sort() for end in range(n - 1, 1, -1): start = 0 mid = end - 1 while (start < mid): prod = (arr[end]*arr[start]*arr[mid]) if (prod > m): mid -= 1 elif (prod < m): start += 1 elif (prod == m): print("YES") print(arr[end],arr[start],arr[mid]) count += 1 mid -= 1 start += 1 return count return count for _ in range(int(input())): n = int(input()) a = printDivisors(n) #print(len(a),a) if countTriplets(a,len(a),n) == 0: print("NO")
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; import java.util.Map.*; public class codeforces { public static void main(String [] args) throws IOException, InterruptedException { Scanner sc=new Scanner(System.in); PrintWriter pw=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { long y=sc.nextLong(); TreeSet<Long> set=printDivisors(y); set.pollFirst(); set.pollLast(); // pw.println(set); if(set.size()<3)pw.println("NO"); else { StringBuilder sb=new StringBuilder(); boolean f=true; long ans=set.first(); sb.append(ans+" "); long k=y/ans; TreeSet<Long> set2=printDivisors(k); set2.pollFirst(); set2.pollLast(); while(!set2.isEmpty() && f) { if(set2.first()!=set2.last() && set2.first()!=ans) {pw.println("YES"+"\n"+ans+" "+set2.first()+" "+set2.last()); f=false; } else if(set2.size()==1) break ; set2.pollFirst(); set2.pollLast(); } if(f)pw.println("NO"); } } pw.flush(); pw.close(); } static TreeSet<Long> printDivisors(long n) { TreeSet<Long> set=new TreeSet<>(); for (long i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) set.add(i); else {set.add(i); set.add(n/i);} } } return set; } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import sys import math import heapq import bisect import re from collections import deque from decimal import * from fractions import gcd def main(): # [int(i) for i in sys.stdin.readline().split()] n = int(sys.stdin.readline()) q = [] for i in range(int(n ** 0.5) + 1, 0, -1): if n % i == 0: q.append(i) if n // i != i: q.append(n // i) q.sort() if len(q) <= 3: print("NO") return 0 i = 1 while i < len(q) - 3: i += 1 f = n / q[1] / q[i] if f != q[1] and f != q[i] and f >= 2 and f == int(f) and int(f) * q[1] * q[i] == n: print("YES") print(q[1], q[i], int(f)) return 0 print("NO") for i in range(int(input())): main()
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math from functools import reduce t = int(input()) for cas in range(t): n = int(input()) ans = [] m = n for i in range(2, int(math.sqrt(m))): cal = 0 if n == 1: break while n % i == 0 and n > 1: n //= i ans.append(i) if n > 1: ans.append(n) if len(ans) < 3: print("NO") elif len(ans) == 3: if len(set(ans)) < 3: print("NO") else: print("YES") print(ans[0], ans[1], ans[2]) elif len(ans) == 4: if len(set(ans)) == 1: print("NO") else: print("YES") print(ans[0], ans[1] * ans[2], ans[3]) elif len(ans) == 5: if len(set(ans)) == 1: print("NO") else: print("YES") print(ans[0], ans[1] * ans[2] * ans[3], ans[4]) else: print("YES") print(ans[0], ans[1] * ans[2], reduce(lambda x, y: x * y, ans[3:]))
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math def fun(s): for i in range(2, int(math.sqrt(s)) + 1): if s % i == 0: w = s // i for j in range(i + 1, int(math.sqrt(w) + 1)): d = w // j if w % j == 0 and d != j and d != i: return i, j, d return -1 for t in range(int(input())): n = int(input()) q = fun(n) if q != -1: print('YES') print(q[0], q[1], q[2]) else: print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t = int(input()) for i in range(t): n = int(input()) a, x, d = [], 0, 2 while d * d <= n and x < 2: if n%d == 0: a.append(d) n = n // d x += 1 d += 1 if n >= d and x == 2: a.append(n) print("YES") res = " ".join(map(str, a)) print(res) else: print("NO")
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
def fun(n): for i in range(2,int(n**0.5)+1): x=n//i if n%i==0: for j in range(2,int(x**0.5)+1): if x%j==0 and x//j!=j and i!=j and x//j!=i: return True,[i,j,x//j] return False,[-1,-1,-1] t=int(input()) for _ in range(t): n=int(input()) res,lst = fun(n) lst.sort() if res: print("YES") print(lst[0],lst[1],lst[2]) else: print("NO")
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int k=0;k<t;k++){ int n=sc.nextInt(); int n1=n; int[] a1=new int[3]; int count=0; for(int i=2;i*i<=n;i++) { if(n%i==0 && (i!=a1[0] || i!=a1[1])) { a1[count]=i; count++; n=n/i; if(count==2 && n>1 && (n!=a1[0] && n!=a1[1])) { a1[2]=n; count++; } } if(count==3) { System.out.println("YES"); System.out.print(a1[0]+" "+a1[1]+" "+a1[2]); System.out.println(); break; } } if(count!=3) { System.out.println("NO"); } } } }
JAVA
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math def primenum(x): count=0 for i in range(2,int(math.floor(math.sqrt(x)))+1): if(x%i==0): count=count+1 if(count==0): return True else: return False t=int(input()) for i in range(t): n=int(input()) m=n if(primenum(n)==True): print('NO') else: l=[] for j in range(2,int(math.sqrt(m))+1): if(n%j==0): l.append(j) n=n//j if(len(l)==2): x=m//(l[0]*l[1]) if(x>l[0] and x>l[1]): l.append(x) if(len(l)==3): if((l[0]*l[1]*l[2])==m): print('YES') print(l[0],l[1],l[2]) break else: print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math class Read: @staticmethod def string(): return input() @staticmethod def int(): return int(input()) @staticmethod def list(sep=' '): return input().split(sep) @staticmethod def list_int(sep=' '): return list(map(int, input().split(sep))) def solve(): n = Read.int() for i in range(2, math.ceil(math.sqrt(n))): if n % i == 0: t = n / i for j in range(2, math.ceil(math.sqrt(t))): if t % j == 0: res = math.ceil(t / j) if res == i or i == j or j == res: continue print('YES') print('{} {} {}'.format(i, j, res)) return print('NO') # query_count = 1 query_count = Read.int() while query_count: query_count -= 1 solve()
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long i, j, k, n; cin >> n; map<long long, long long> mp; long long x = n; while (x % 2 == 0) { mp[2]++; x /= 2; } for (i = 3; i * i <= x; i += 2) { while (x % i == 0) { mp[i]++; x /= i; } } if (x > 1) mp[x]++; long long a = 1, b = 1, c = 1; i = 0; auto it = mp.begin(); a = it->first; if (it->second > 2) { b = it->first * it->first; c = n / (it->first * it->first * it->first); } else if (it->second == 2) { b = it->first; it++; if (it == mp.end()) { cout << "NO\n"; continue; } else { b *= it->first; c = n / (a * b); } } else { it++; if (it == mp.end()) { cout << "NO\n"; continue; } else { b *= it->first; c = n / (a * b); } } if (a == b || b == c || c == a || a < 2 || b < 2 || c < 2) { cout << "NO\n"; } else { cout << "YES\n"; cout << a << " " << b << " " << c << "\n"; } } }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t=int(input()) for _ in range(t): n=int(input()) a,b,c=0,0,0 flag=0 flg=0 cnt=0 mx=2 l=[] if(n<24): print("NO") else: for _ in range(3): for i in range(mx, int(n**0.5) + 1): if n % i == 0: flg=1 break if(flg==0): print("NO") flag=1 break mx=i+1 n=n//i flg=0 if(i<n): l.append(i) cnt+=1 else: print("NO") flag=1 break if(cnt==2): l.append(n) break if(flag==0): print("YES") print(l[0],l[1],l[2])
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
from math import sqrt N = int(input()) for i in range(N): n = int(input()) res = [] is_good = False for i in range(2, n//6 + 1): if i > sqrt(n): break if n % i == 0: res.append(i) n /= i if len(res) == 2: if n > i: is_good = True break if is_good: print(f'YES\n{res[0]} {res[1]} {int(n)}') else: print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t = int(input()) from math import * import bisect for i in range(t): n = int(input()) x = set() for j in range(2,int(sqrt(n))+1): if(n%j==0): x.add(j) if(n%(n//j)==0): x.add(n//j) x = list(x) x.sort() flag = 0 for j in range(len(x)): for k in range(j+1,len(x)): y = n/(x[j]*x[k]) if(y in x and y!=x[j] and y!=x[k]): flag = 1 print('YES') print(x[j],x[k],int(y)) break if(flag==1): break if(flag == 0): print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t = int(input()) for _ in range(t): n = int(input()) ans = [] i = 2 top = 10**6 while len(ans) < 2 and i <= n and i <= top: if n%i == 0: n //= i ans.append(i) i += 1 if len(ans) < 2 or n == 1 or n in ans: print('NO') else: print('YES') ans.append(n) for i in ans: print(i, end=' ') print()
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> f, c; int r = n; int co{0}; while (r % 2 == 0) { r = r / 2; co++; } if (co > 0) { f.push_back(2); c.push_back(co); } for (int i = 3; i * i < n; i++) { co = 0; while (r % i == 0) { r = r / i; co++; } if (co > 0) { f.push_back(i); c.push_back(co); } } if (r > 2) { f.push_back(r); c.push_back(1); } if (f.size() >= 3) { cout << "YES\n"; cout << f[0] << ' ' << f[1] << ' '; n = n / f[0]; n = n / f[1]; cout << n << '\n'; } else if (f.size() == 2) { if (c[0] + c[1] >= 4) { cout << "YES\n"; cout << f[0] << ' ' << f[1] << ' '; n = n / f[0]; n = n / f[1]; cout << n << '\n'; } else { cout << "NO\n"; } } else { if (c[0] > 5) { cout << "YES\n"; cout << f[0] << ' ' << f[0] * f[0] << ' '; n = n / f[0]; n = n / f[0]; n = n / f[0]; cout << n << '\n'; } else { cout << "NO\n"; } } } return 0; }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
t=int(input()) for t in range (t): n=int(input()) d=2 cate=0 vec=[0]*10 while d*d <= n: if(n%d == 0 and cate<2): vec[cate]=d cate+=1 n/=d if cate == 2: break d+=1 if n > 1 and cate == 2: vec[cate]=n cate+=1 if cate < 3 or vec[2] == vec[1]: print("NO") else: print("YES") print(int(vec[0]),int(vec[1]),int(vec[2]))
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
import math def primeFactors(n): f = [] while n % 2 == 0: f.append(2), n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: f.append(i), n = n / i if n > 2: f.append(n) return f t = int(input()) while t>0: n = int(input()) l = primeFactors(n) s = list(set(l)) ss = len(s) sl = len(l) if sl<=2: print('NO') elif ss==1 and sl<6: print('NO') elif ss==2 and sl<4: print('NO') else: if ss>=2: l1 = [int(s[0]),int(s[1]),int(n/(s[0]*s[1]))] print('YES') print(*l1) else: l2 = [int(s[0]),int(s[0]*s[0]),int(n/(s[0]*s[0]*s[0]))] print('YES') print(*l2) t-=1
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
# -*- encoding: utf-8 -*- import sys import math r_input = sys.stdin.readline def get_divisor(num): divisors = [] length = int(math.sqrt(num)) + 1 for i in range(1, length): if num % i == 0: divisors.append(i) divisors.append(num // i) return divisors if __name__ == '__main__': T = int(r_input()) for _ in range(T): number = int(r_input()) f_list = get_divisor(number) flag = 0 for n in f_list: tp = number // n s_list = get_divisor(tp) for m in s_list: tmp = sorted([n, m, tp // m]) if not 1 in tmp: if tmp[0] != tmp[1] and tmp[1] != tmp[2]: print('YES') print(*tmp) flag = 1 break if flag: break if not flag: print('NO')
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int MAXN = (int)1e5 + 5; const int MOD = (int)1e9 + 7; const int INF = 0x3f3f3f3f; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; bool mul(int n, int& a, int& b, int& c) { a = INF, b = INF, c = INF; for (int i = 2; i * i * i <= n; i++) { if (n % i == 0) { a = i; break; } } if (n % a == 0) n = n / a; else return false; for (int i = a + 1; i * i <= n; i++) { if (n % i == 0) { b = i; break; } } if (n % b == 0) n = n / b; else return false; if (n > b) { c = n; return true; } else return false; } int main() { int T, a, b, c, n; cin >> T; while (T--) { cin >> n; if (mul(n, a, b, c)) printf("YES\n%d %d %d\n", a, b, c); else printf("NO\n"); } return 0; }
CPP
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
from sys import stdin,stdout t = 1 t=int(input()) def factorgreaterthan(n,x): i=x+1 while (i*i)<=n: if n%i==0: return i i+=1 return n for i in range(t): n=int(input()) N=n l=[] d1=factorgreaterthan(n,1) n//=d1 l.append(d1) d2=factorgreaterthan(n,d1) n//=d2 l.append(d2) l.append(n) l.sort() m=1 flag=True p=1 for i in l: m*=i if i==p: flag=False p=i if m!=N: flag=False if flag: print("YES") print(*l) else: print("NO") #solve
PYTHON3
1294_C. Product of Three Numbers
You are given one integer number n. Find three distinct integers a, b, c such that 2 ≀ a, b, c and a β‹… b β‹… c = n or say that it is impossible to do it. If there are several answers, you can print any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2 ≀ n ≀ 10^9). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent n as a β‹… b β‹… c for some distinct integers a, b, c such that 2 ≀ a, b, c. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823
2
9
def Divisors(number): Div=set() sqr=int(number**0.5) for i in range(2,sqr+1): if number%i==0: Div=Div|{i} Div=Div|{(number//i)} return list(Div) def getDiv(Number): D=list(Divisors(Number)) D.sort() for i in range(len(D)): for j in range(i+1,len(D)): m=D[i]*D[j] if m in D and Number//m !=D[i] and Number//m !=D[j] : r=str(D[i])+' '+str(D[j])+' '+str(Number//m) r='YES\n'+r return r return 'NO' Answers=[] n=int(input()) for i in range(n): a=int(input()) Answers.append(getDiv(a)) for i in Answers: print(i)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; void err() { cout << endl; } template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); } using ll = long long; using db = double; using pII = pair<int, int>; const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const int N = 2e2 + 10; int n; int a[N], b[N]; int vis[N]; void RUN() { memset(vis, 0, sizeof vis); cin >> n; for (int i = 1; i <= n; ++i) { cin >> b[i]; } for (int i = 1, j = 1; i <= n; ++i, j += 2) { a[j] = b[i]; vis[b[i]] = 1; } for (int i = 2; i <= 2 * n; i += 2) { int lst = -1; for (int j = a[i - 1] + 1; j <= 2 * n; ++j) { if (!vis[j]) { vis[j] = 1; lst = j; break; } } if (lst == -1) { cout << -1 << "\n"; return; } a[i] = lst; } for (int i = 1; i <= 2 * n; ++i) { cout << a[i] << " \n"[i == 2 * n]; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cout << fixed << setprecision(20); int T; cin >> T; for (int cas = 1; cas <= T; ++cas) { RUN(); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.LinkedList; public class Unusual_Competitions { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t1=Integer.parseInt(br.readLine()); for(int t=0;t<t1;++t) { int n=Integer.parseInt(br.readLine()); ArrayList<Integer>al=new ArrayList<>(); StringBuilder ans=new StringBuilder(); for(int i=1;i<=2*n;++i) { al.add(i); } String s[]=br.readLine().trim().split(" "); for(String a:s) { Integer k=new Integer(a); al.remove(k); } boolean flag=true; for(String a:s) { Integer k=new Integer(a); ans.append(a+" "); int f=0; for(Integer i:al) { if(i>k) { f=1; ans.append(i+" "); al.remove(i); break; } } if(f==0) { System.out.println(-1); flag=false; break; } } if (flag) { System.out.println(ans); } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) from collections import defaultdict for _ in range(t): dic = defaultdict(int) dic2 = defaultdict(int) n = int(input()) bl = list(map(int,input().split())) for idx, b in enumerate(bl): dic[b] = idx + 1 bl_sort = sorted(bl) s = 0 kouho = [] for b in range(1, 2*n+1): if not dic[b]: if len(kouho) == 0: print(-1) break min_idx = float("inf") pos = -1 for k in kouho: if min_idx > dic[k]: min_idx = dic[k] pos = k dic2[pos] = b kouho.remove(pos) else: kouho.append(b) s += 1 else: ansl = [] for b in bl: ansl.append(b) ansl.append(dic2[b]) print(*ansl) continue
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; long long int modPow(long long int x, long long int n, long long int mod) { long long int r = 1; x = x % mod; while (n) { if (n & 1) r = (r * x) % mod; n = n >> 1; x = (x * x) % mod; } return r; } int main() { long long int t, i, j, k, l, n, m, a, b, c, x, y, z; cin >> t; while (t--) { cin >> n; vector<long long int> V; map<long long int, long long int> M; bool ans = true; for (i = 0; i < n; ++i) { cin >> l; M[l]++; V.push_back(l); if (M[l] > 1) ans = false; } vector<long long int> P(2 * n, 0); for (i = 0; i < n; ++i) P[i * 2] = V[i]; set<pair<long long int, long long int> > S; for (i = 0; i < n; ++i) { bool milla = false; for (j = V[i]; j <= 2 * n; ++j) { if (M[j] == 0) { M[j]++; P[2 * i + 1] = j; milla = true; break; } } if (milla == false) { ans = false; break; } } if (ans) { for (auto it : P) cout << it << " "; cout << "\n"; } else { cout << -1 << "\n"; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for q in range(t): n=int(input()) arr=list(map(int,input().split())) marked=[0]*(2*n+1) for i in range(len(arr)): marked[arr[i]]=1 #print(marked) ans=[] for i in range(n): ans.append(arr[i]) for j in range(arr[i]+1,len(marked)): if marked[j]==0: ans.append(j) flag=1 marked[j]=1 break if len(ans)==2*n: for i in range(len(ans)): print(ans[i],end=" ") print() else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int N = 105; int T, n, tot; int a[N], b[N << 1]; bool vis[N << 1]; int main() { scanf("%d", &T); while (T--) { memset(vis, false, sizeof(vis)); scanf("%d", &n); for (register int i = 1; i <= n; ++i) scanf("%d", &a[i]), vis[a[i]] = true; bool jay = true; for (register int i = 1; i <= n; ++i) if (a[i] == 2 * n) { jay = false; break; } if (!jay) { puts("-1"); continue; } int tot = 0; for (register int i = 1; i <= n; ++i) { bool flag = false; for (register int j = a[i] + 1; j <= 2 * n; ++j) if (!vis[j]) { b[++tot] = a[i]; b[++tot] = j; vis[a[i]] = vis[j] = true; flag = true; break; } if (!flag) { jay = false; break; } } if (!jay) { puts("-1"); continue; } for (register int i = 1; i <= tot; ++i) printf("%d ", b[i]); puts(""); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2") using namespace std; const double eps = 1E-9; const double Exp = 2.718281828459045; const double Pi = 3.1415926535897932; const int Max_Bit = 63; const int INF = 2000000000; const long long LINF = 1000000000000000007ll; const int MOD = 1000000007; const int NMAX = 1000005; const int MMAX = 505; const int KMAX = 6; const string CODE = "CODEFORCES"; int t[4 * NMAX]; void build(vector<int> a, int v, int tl, int tr) { if (tl == tr) t[v] = a[tl]; else { int tm = (tl + tr) / 2; build(a, v * 2, tl, tm); build(a, v * 2 + 1, tm + 1, tr); t[v] = t[v * 2] + t[v * 2 + 1]; } } int sum(int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (l == tl && r == tr) return t[v]; int tm = (tl + tr) / 2; return sum(v * 2, tl, tm, l, min(r, tm)) + sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); } long long gcd(long long a, long long b, long long& x, long long& y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; for (cin >> T; T; T--) { int n, a[100], b[200]; int o = 0; priority_queue<int> q; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i <= 2 * n; ++i) { int fnd = 0; for (int j = 0; j < n; ++j) if (a[j] == i) fnd = j + 1; if (fnd == 0) { if (q.empty()) { o = 1; break; } int pos = -q.top(); q.pop(); b[pos] = i; } else { b[fnd * 2 - 2] = i; q.push(-fnd * 2 + 1); } } if (o) { cout << -1 << endl; continue; } for (int i = 0; i < 2 * n; ++i) cout << b[i] << " "; cout << "\n"; ; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for i in range(t): n=int(input()) arr=list(map(int,input().split())) ans=[] for i in range(1,len(arr)*2+1): if i not in arr: ans.append(i) fina=[] for i in range(len(arr)): fina.append(arr[i]) for j in range(len(ans)): if ans[j]>arr[i]: fina.append(ans[j]) ans.remove(ans[j]) break if len(fina)!=2*n: print(-1) else: print(*fina,sep=" ")
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; public class Solutions { static Scanner scr=new Scanner(System.in); public static void main(String[] args) { int t=read(); while(t-->0) { solve(); } } static void solve() { int n=read(); int []a=new int[n]; int []count=new int[2*n+1]; for(int i=0;i<n;i++) { a[i]=read(); count[a[i]]=1; } int []res=new int[2*n]; for(int i=0;i<n;i++) { res[2*i]=a[i]; } int index=1; for(int i=0;i<n;i++) { int num=a[i]; int curr=a[i]+1; while(curr<=2*n) { if(count[curr]==0) { count[curr]=1; res[index]=curr; index+=2; break; }else { curr++; } } if(curr==2*n+1) { println(-1); return; } } printArray(res); println(); } static int[] sort(int a[]) { Arrays.sort(a); return a; } static String reverse(String s) { String res=""; for(int i=s.length()-1;i>=0;i--) { res+=s.charAt(i); } return res; } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static void swap(int a[],int i,int j) { int temp=a[i]; a[i]=a[j]; a[j]=temp; } static boolean contains(HashMap<Integer,Integer>hm,int ele) { return hm.containsKey(ele); }static boolean contains(HashMap<Character,Integer>hm,char ele) { return hm.containsKey(ele); }static int get(HashMap<Character,Integer>hm,char ele) { return hm.get(ele); } static char[] cha(String s) { return s.toCharArray(); } static int len(String s) { return s.length(); } static int len(int a[]) { return a.length; } static int len(long a[]) { return a.length; } static long abs(long a) { return Math.abs(a); }static int abs(int a) { return Math.abs(a); } static long min(long a,long b) { return Math.min(a, b); }static long max(long a,long b) { return Math.max(a, b); } static void print(int a) { System.out.print(a+" "); }static void print(long a) { System.out.print(a+" "); }static void print(String a) { System.out.print(a); }static void println(int a) { System.out.println(a); }static void println(long a) { System.out.println(a); }static void println(String a) { System.out.println(a); } static void println() { System.out.println(); } static void println(char []c) { for(int i=0;i<c.length;i++) { System.out.print(c[i]); } println(); } static int read() { return scr.nextInt(); } static String readS() { return scr.next(); } static long readL() { return scr.nextLong(); } static int[]readIntArray(int n){ // int n=read(); int []a=new int[n]; for(int i=0;i<n;i++) { a[i]=scr.nextInt(); } return a; }static long[]readLongArray(int n){ long []a=new long[n]; for(int i=0;i<n;i++) { a[i]=scr.nextLong(); } return a; } static void printArray(int a[]) { int n=a.length; for(int i=0;i<n;i++) { print(a[i]); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
# import sys # input = sys.stdin.readline # from bisect import bisect_left, bisect_right # INF = 1 << 60 t = int(input()) for i in range(t): n = int(input()) b = list(map(int, input().split())) c = [0 for i in range(2 * n)] ans = [] possible = True for j in b: c[j - 1] = 1 for j in b: possible2 = False for k in range(j, 2 * n): if c[k] == 0: c[k] = 1 ans.append(j) ans.append(k + 1) possible2 = True break if not possible2: possible = False break if not possible: print(-1) # print(ans) else: print(*ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for _ in range(t): n=int(input()) b=[int(s) for s in input().split()] vmax=max(b) vmin=min(b) a=[] for i in range(0,n): a.append(b[i]) k=b[i] while k in b or k in a: k+=1 if k>2*n: print(-1) break a.append(k) else: print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T-->0) { int N = sc.nextInt(); int[] list = new int[N+1]; HashSet<Integer> set = new HashSet<>(); for(int i =1 ; i <= N;i++) { list[i] = sc.nextInt(); set.add(list[i]); } int[] finalList = new int[2*(N+1)]; Arrays.fill(finalList , -1); for(int i = 1 ; i <= 2*N ; i+=2) { int idx = (int)(Math.ceil(i/2d)); finalList[i] = list[idx]; } boolean flag = true; for(int i = 1 ; i <= N ; i++) { int bi = list[i]; int min = Math.min(finalList[2*i],finalList[(2*i)-1]); if(min>bi) { flag = false; break; } //find the number for(int k = Math.max(Math.max(finalList[2*i],finalList[(2*i)-1]) , 0) ;k<=2*N;k++) { // if(!set.contains(k)) { //bzrsh too finalList[2*i] = k; set.add(k); break; } } } if(!flag) System.out.println(-1); else { StringBuilder sb = new StringBuilder(); for(int i = 1; i <= 2*N ; i++) { if(finalList[i] == -1) { flag = false; System.out.println(-1); break; } sb.append(finalList[i]+" "); } if(flag) { sb.deleteCharAt(sb.length()-1); System.out.println(sb.toString()); } } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.io.*; public class Main { static StringBuilder sb = new StringBuilder(); static BufferedReader br = null; static StringTokenizer st = null; static int n; static int[] ba; static boolean[] ck; public static void main(String[] args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); st = null; int tc = Integer.parseInt(br.readLine()); while(tc-- >0) { n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); ba = new int[n+1]; ck = new boolean[2*n+2]; boolean flg = false; for(int i=1; i<=n; i++) { ba[i] = Integer.parseInt(st.nextToken()); if(ba[i]>=2 * n) { flg = true; }else { ck[ba[i]]=true; } } StringBuilder ans = new StringBuilder(); for(int i=1; i<=n; i++) { ans.append(ba[i]).append(" "); int tmp = ++ba[i]; while(ck[tmp] && tmp<=2*n) { tmp++; } if(tmp<=2*n) { ans.append(tmp).append(" "); ck[tmp]=true; }else { flg = true; break; } } if(flg) sb.append("-1").append("\n"); else sb.append(ans).append("\n"); } System.out.println(sb); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def solve(n,b): if 1 not in b or 2*n in b: return ([-1]) else: s=set(b) arr=[] for i in range(n): j=b[i]+1 while True: if j not in s and j<=2*n: arr.append(j) s.add(j) break elif j>2*n: return ([-1]) j+=1 return ([str(b[x])+" "+str(arr[x]) for x in range(n)]) for _ in range(int(input())): n=int(input()) b=[int(x) for x in input().split()] print(*solve(n,b))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from bisect import bisect for _ in range(input()): n = input() a = map(int,raw_input().split()) if 1 not in a or 2*n in a: print -1 continue l = [] for i in range(1,2*n+1): if i not in a: l.append(i) d = {} f = 0 for i in range(n): p = bisect(l,a[i]) if p==len(l): f = 1 break d[a[i]] = l[p] l.remove(l[p]) if f==1: print -1 continue for i in range(n): print a[i],d[a[i]], print
PYTHON
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import bisect t=int(input()) def find_gt(a, x): 'Find leftmost value greater than x' i = bisect.bisect_right(a, x) if i != len(a): return a[i] raise ValueError for _ in range(t): b=int(input()) B=list(map(int,input().split())) d={} v=[0] x=[] y=[] for i in range(1,2*b+1): v.append(0) for i in B: v[i]=1 for i in range(1,2*b+1): if(v[i]==0): y.append(i) if(v[i]): x.append(i) i=0 f=1 while(i<b): if(x[i]<y[i]): d[x[i]]=y[i] else: print(-1) f=0 break i+=1 if(f): for i in B: s=find_gt(y,i) y.remove(s) print(i,s,end=" ") print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class C implements Runnable { private void solve() throws IOException { int t = nextInt(); outer2: while(t-- > 0) { int n = nextInt(); int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = nextInt(); } boolean[] vis = new boolean[2 * n + 1]; int[] ans = new int[2 * n]; for (int i = 0; i < b.length; i++) { ans[2 * i] = b[i]; vis[b[i]] = true; } outer: for (int i = 1; i < ans.length; i += 2) { for (int j = ans[i - 1] + 1; j <= 2 * n; j++) { if (!vis[j]) { ans[i] = j; vis[j] = true; continue outer; } } pl(-1); continue outer2; } for (int o : ans) p(o + " "); pl(); } } public static void main(String[] args) { new C().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new BufferedReader(new InputStreamReader(System.in))); writer = new PrintWriter(System.out); tokenizer = null; solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } void p(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } void pl(Object... objects) { p(objects); writer.println(); } int cc; void pf() { writer.printf("Case #%d: ", ++cc); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(2 * n + 1), b(n + 1), c(2 * n + 1, 0); set<int> d; for (int i = 1; i <= n; i++) { cin >> b[i]; c[b[i]] = 1; } for (int i = 1; i <= 2 * n; i++) if (!c[i]) { d.insert(i); } bool ok = true; for (int i = 1; i <= n; i++) { auto it = lower_bound(d.begin(), d.end(), b[i] + 1); if (it == d.end()) { ok = false; break; } else { a[2 * i - 1] = b[i]; a[2 * i] = *it; d.erase(it); } } if (ok) { for (int i = 1; i <= 2 * n; i++) { cout << a[i] << (i < 2 * n ? ' ' : '\n'); } } else { cout << -1 << endl; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys,os,io # input = sys.stdin.readline # for strings # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # for non-strings PI = 3.141592653589793238460 INF = float('inf') MOD = 1000000007 # MOD = 998244353 def bin32(num): return '{0:032b}'.format(num) def add(x,y): return (x+y)%MOD def sub(x,y): return (x-y+MOD)%MOD def mul(x,y): return (x*y)%MOD def gcd(x,y): if y == 0: return x return gcd(y,x%y) def lcm(x,y): return (x*y)//gcd(x,y) def power(x,y): res = 1 x%=MOD while y!=0: if y&1 : res = mul(res,x) y>>=1 x = mul(x,x) return res def mod_inv(n): return power(n,MOD-2) def prob(p,q): return mul(p,power(q,MOD-2)) def ii(): return int(input()) def li(): return [int(i) for i in input().split()] def ls(): return [i for i in input().split()] for t in range(ii()): n = ii() a = li() b = [0 for i in range(2*n)] for i in range(n): b[2*i] = a[i] # print(b) inds = [-1 for i in range(2 * n + 1)] for i in range(n): inds[a[i]] = i a.sort() a.reverse() lol = 1 for i in a: ind = inds[i] flag = 0 for j in range(2*n , i, -1): if inds[j ] == -1: flag = j if flag == 0: lol = 0 break b[2*ind + 1] = flag inds[flag] = 2*ind + 1 if lol: # print(b) for i in range(n): for j in range(i + 1, n): if b[2*i + 1] > b[2*j] and b[2*j + 1] > b[2*i] and b[2*i + 1] > b[2*j+1]: b[2*i + 1] ,b[2*j + 1] = b[2*j + 1] , b[2*i + 1] # print(i , j) print(*b) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int arr[1005]; int counter[1005]; int main() { int tc; cin >> tc; while (tc--) { memset(arr, 0, 1005); memset(counter, 0, 1005); int flag = 0; vector<int> v; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; counter[arr[i]]++; } int lim = 2 * n; for (int i = 0; i < n; i++) { v.push_back(arr[i]); for (int j = arr[i] + 1;; j++) { if (j > lim) { flag = 1; break; } if (!counter[j]) { counter[j]++; v.push_back(j); break; } } } if (flag == 1) cout << "-1" << endl; else { for (auto it = v.begin(); it != v.end(); ++it) cout << *it << " "; cout << endl; } } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys input = sys.stdin.readline t=int(input()) for tests in range(t): n=int(input()) B=list(map(int,input().split())) USE=[0]*(2*n+1) A=[-1]*(2*n) for i in range(n): A[2*i]=B[i] USE[B[i]]=1 for i in range(2*n): if A[i]!=-1: continue for j in range(A[i-1]+1,2*n+1): if USE[j]==0: A[i]=j USE[j]=1 break if -1 in A: print(-1) else: print(*A)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n=int(input()) b=[int(i) for i in input().strip().split()] taken=set(b) lim=2*n a=[] for x in b: done=False pairx=None for y in range(x+1,lim+1): if y not in taken: done=True pairx=y break if done==False: break a+=[x,pairx] taken.add(pairx) if done==False: print(-1) else: for x in a: print(x," ",sep='',end='') print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; int vis[205], p = 0; memset(vis, 0, sizeof(vis)); while (t--) { ++p; int n, b[205]; cin >> n; for (int i = 0; i < n; ++i) { cin >> b[i]; vis[b[i]] = p; } int index; bool f = false; vector<int> ans; for (int i = 0; i < n; ++i) { index = -1; for (int j = b[i] + 1; j <= 2 * n; ++j) { if (vis[j] != p) { index = j; break; } } if (index == -1) { f = true; break; } vis[index] = p; ans.push_back(b[i]); ans.push_back(index); } if (!f) { for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " "; } else cout << -1; cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) while(t): t-=1 n=int(input()) li=list(map(int,input().split())) b_li=[0]*(2*n) for i in range(0,n): b_li[i*2]=li[i] have=[] for i in range(1,2*n+1): if i not in li: have.append(i) for i in range(1,2*n+1,2): pre=b_li[i-1] flag=0 for j in range(len(have)): if have[j]>pre: flag=1 break if flag==1: b_li[i]=have[j] del(have[j]) else: print(-1) break if (flag==1): for i in b_li: print(i,end=" ") print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.lang.*; import java.io.*; public class cf { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O using short named function ---------*/ public static String ns() { return scan.next(); } public static int ni() { return scan.nextInt(); } public static long nl() { return scan.nextLong(); } public static double nd() { return scan.nextDouble(); } public static String nln() { return scan.nextLine(); } public static void p(Object o) { out.print(o); } public static void ps(Object o) { out.print(o + " "); } public static void pn(Object o) { out.println(o); } /*-------- for output of an array ---------------------*/ static void iPA(int arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void lPA(long arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void sPA(String arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void dPA(double arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ni(); } static void lIA(long arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nl(); } static void sIA(String arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ns(); } static void dIA(double arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nd(); } /*------------ for taking input faster ----------------*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static ArrayList<Integer> sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> arr=new ArrayList<>(); // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Method to check if x is power of 2 static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } //Method to return lcm of two numbers static int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n) { String sex = Long.toString(n); return sex.length(); } static void reverse(int a[]) { int i, k, t; int n = a.length; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } //Method for sorting static void ruffle_sort(int[] a) { //shandom_ruffle Random r = new Random(); int n = a.length; for (int i = 0; i < n; i++) { int oi = r.nextInt(n); int temp = a[i]; a[i] = a[oi]; a[oi] = temp; } //sort Arrays.sort(a); } //Method for checking if a number is prime or not static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void main(String[] args) throws java.lang.Exception { OutputStream outputStream = System.out; out = new PrintWriter(outputStream); scan = new FastReader(); //for fast output sometimes StringBuilder sb = new StringBuilder(); int t = ni(); String s="1"; while(t-- != 0) { int n=ni(); int[] a=new int[n]; iIA(a); HashSet<Integer> hs=new HashSet<>(); int count=0;int f=0;int[] b=new int[n*2]; for(int i=0;i<n;i++){ if(a[i]>n) count++; if(count>n/2){ f=1;break; } } if(f==1){ pn("-1"); } else{ int j=0; for(int i:a) hs.add(i); for(int i=0;i<n;i++){ b[j++]=a[i]; int tt=a[i]+1; while(hs.contains(tt)) tt+=1; hs.add(tt); b[j++]=tt; } int max=0; for(int i:b) max=Math.max(max,i); if(max>2*n) pn(-1); else{ iPA(b); } } } out.flush(); out.close(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) b = [i for i in range(1,2*n+1) if i not in a] sol = [0]*(2*n) for i in range(len(a)): tmp=[j for j in b if j>a[i]] if len(tmp)==0: sol=-1 break else: sol[2*i]=a[i] sol[2*i+1]=tmp[0] b.remove(tmp[0]) if sol==-1: print(sol) else: print(' '.join([str(i) for i in sol]))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int t, n; int main() { cin >> t; while (t--) { int f = 0; cin >> n; vector<int> a; a.push_back(0); map<int, int> m; int b[2 * n + 5]; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x >= 2 * n) f = 1; a.push_back(x); m[x]++; } if (f) { cout << -1 << endl; continue; } for (int i = 1; i <= n && !f; i++) { int x = a[i]; while (m.find(x) != m.end()) x++; if (x > 2 * n) { f = 1; } else { m[x]++; b[2 * i - 1] = a[i]; b[2 * i] = x; } } if (f) cout << "-1" << endl; else { for (int i = 1; i <= 2 * n; i++) { cout << b[i] << " "; } cout << endl; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 1000000007; const int MAXN = (int)3e5 + 1; void setIO(string name) { ios_base::sync_with_stdio(0); cin.tie(0); freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } int main() { int t; cin >> t; while (t--) { int n; cin >> n; bool used[2 * n]; for (int i = 0; i < (2 * n); i++) { used[i] = false; } int seq[n]; for (int i = 0; i < n; i++) { cin >> seq[i]; seq[i]--; used[seq[i]] = true; } int ans[2 * n]; bool can = true; for (int i = 0; i < n; i++) { int val = seq[i]; while (val < 2 * n && used[val]) { val++; } if (val == 2 * n) { can = false; break; } used[val] = true; ans[(2 * i)] = seq[i]; ans[(2 * i + 1)] = val; } if (!can) { cout << -1; } else { for (int i = 0; i < 2 * n; i++) { cout << ans[i] + 1 << " "; } } cout << "\n"; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = -1e-2; const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; const long long mod = 1e9 + 7; const int Mx = INT_MAX; const int Mn = INT_MIN; const long long MX = LLONG_MAX; const long long MN = LLONG_MIN; const int N = 1e5 + 9; void Open() {} long long arr[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); Open(); int t; cin >> t; while (t--) { set<int> st; set<int>::iterator it; ; int n; cin >> n; vector<int> v(n + 1), ans(2 * n + 1); for (int i = 1; i <= 2 * n; i++) st.insert(i); for (int i = 1; i <= n; i++) { cin >> v[i]; st.erase(v[i]); } bool ok = 1; for (int i = 1; i <= n; i++) { it = st.upper_bound(v[i]); if (it == st.end()) { ok = 0; break; } ans[2 * i - 1] = v[i]; ans[2 * i] = *it; st.erase(it); } if (ok) { for (int i = 1; i <= 2 * n; i++) cout << ans[i] << " "; } else cout << -1; cout << "\n"; } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#import sys #import math #sys.stdout=open("C:/Users/pipal/OneDrive/Desktop/VS code/python/output.txt","w") #sys.stdin=open("C:/Users/pipal/OneDrive/Desktop/VS code/python/input.txt","r") t=int(input()) for i in range(t): n=int(input()) l=list(map(int,input().split())) if 1 not in l: print("-1") elif 2*n in l: print(-1) else: new=[] for i in range(n): chc=1 new.append(l[i]) for k in range(l[i],2*n+1): if k not in l: l.append(k) new.append(k) chc=0 break if chc==1: print(-1) break if chc==0: for i in range(len(new)): print(new[i],end=' ') print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
I=input for _ in[0]*int(I()): n=2*int(I());a=[0]*n;b=a[::2]=*map(int,I().split()),;c={*range(1,n+1)}-{*b};i=1 try: for x in b:y=a[i]=min(c&{*range(x,n+1)});c-={y};i+=2 except:a=-1, print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def solve(N, B): A = [0] * (2 * N) check = [False] * (2 * N) flag = True for i in range(N): A[i*2] = B[i] check[B[i]-1] = True for i in range(N): tmp = A[i*2] for j in range(tmp, 2*N): if not check[j]: A[i*2+1] = j+1 check[j] = True break for i in range(N): if A[2*i] > A[2*i+1]: flag = False if flag: print(" ".join(map(str, A))) else: print(-1) def main(): T = int(input()) for i in range(T): N = int(input()) B = list(map(int, input().split())) solve(N, B) if __name__ == "__main__": main()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import math import heapq import sys num = int(raw_input()) for i in range(num): n = int(raw_input()) nums = [int(x) for x in raw_input().split(" ")] if 1 not in nums: print("-1") continue cur_set = set() for nn in nums: cur_set.add(nn) ans = [] flag = 0 for nn in nums: ans.append(str(nn)) c = nn+1 while c in cur_set and c<=2*n: c+=1 if c>2*n: flag = 1 break ans.append(str(c)) cur_set.add(c) if flag == 1: print("-1") continue print(" ".join(ans))
PYTHON
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int mod = 1e9 + 7; const char nl = '\n'; void solve() { int t; cin >> t; vector<int> b, a; unordered_map<int, int> all; while (t--) { int n; cin >> n; b.resize(n); all.clear(); bool all_unique = true; for (int i = 0; i < n; ++i) { cin >> b[i]; if (all.find(b[i]) != all.end()) { all_unique = false; } all[b[i]] = i; } if (!all_unique) { cout << -1 << "\n"; continue; } a.assign(2 * n, -1); bool good = true; for (int m = 1; m <= 2 * n; ++m) { if (all.find(m) != all.end()) { int pos = all[m]; a[2 * pos] = m; } else { bool found = false; int ind = 0; for (int i = 0; i < n; ++i) { if (a[2 * i] != -1 && a[2 * i + 1] == -1) { found = true; ind = i; break; } } if (found) { a[2 * ind + 1] = m; } else { good = false; break; } } } if (!good) { cout << -1; } else { for (int i = 0; i < 2 * n; ++i) { cout << a[i] << " "; } } cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for i in range(0,t): o=[] n=int(input()) l=list(map(int,input().split())) e=0 for j in range(0,len(l)): o.append(l[j]) c=l[j]+1 if c>2*n: e=1 break else: while c in l or c in o: c+=1 if c>2*n: e=1 break if e==1: break o.append(c) if e==1: print(-1) else: for k in range(0,len(o)): print(o[k],end=' ') print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for T in range(int(input())): n = int(input()) b = list(map(int, input().split())) Ans = [] Exist = [False] * (2 * n + 100) Bad = False for i in b : Exist[i] = True for i in range(n): Ans.append(b[i]) Number = b[i] + 1 while Exist[Number] == True and Number < 2 * n: Number += 1 if Number <= 2 * n and Exist[Number] == False: Ans.append(Number) Exist[Number] = True else: Bad = True break if Bad : print(-1) else : print(*Ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; set<int> prime; void SieveOfEratosthenes(int n) { bool prme[n + 1]; memset(prme, true, sizeof(prme)); for (int p = 2; p * p <= n; p++) { if (prme[p] == true) { for (int i = p * p; i <= n; i += p) prme[i] = false; } } for (int p = 2; p <= n; p++) if (prme[p]) prime.insert(p); } void solve() { int n; cin >> n; vector<int> in(n), v; set<int> s, temp; for (int i = 0; i < n; i++) { cin >> in[i]; s.insert(in[i]); } for (auto i : in) { v.push_back(i); int x = i + 1; while (s.count(x) || temp.count(x)) { x++; } v.push_back(x); temp.insert(x); } auto it = temp.end(); it--; if ((*it) != (2 * n)) { cout << -1; } else { for (auto i : v) cout << i << ' '; } cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) solve(); }
CPP