UWCOI21A - Hidden Numbers - Editorial

PROBLEM LINK:

Contest
Practice

Author: Leo Lee
Tester: Daanish Mahajan
Editorialist: Leo Lee

DIFFICULTY:

Cakewalk

PREREQUISITES:

Math

PROBLEM:

There are T testcases. For each testcase, you are given an integer N. Find two integers A and B such that A * B = N.

Subtask 1 [100 points]: 1 \le T \le 10^5, 1 \le N \le 10^9

QUICK EXPLANATION:

A = 1, B = N.

EXPLANATION:

Subtask 1:

We can use the property that every number times 1 is itself. That means we can always set one number as 1, making the other number N.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        cout << "1 " << n << endl;
    }
}
Tester's Solution
import java.util.*;
import java.io.*;
public class Main{
    static PrintWriter out;
    static InputReader in;
    public static void main(String args[]){
        out = new PrintWriter(System.out);
        in = new InputReader();
        new Main();
        out.flush(); out.close();
    }   
    Main(){
        solve();
    }
    void solve(){
    	int t = in.nextInt();
    	while(t-- > 0){
    		int n = in.nextInt();
    		out.println(1 + " " + n);
    	}
    }
    public static class InputReader{
        BufferedReader br;
        StringTokenizer st;
        InputReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        public int nextInt(){
            return Integer.parseInt(next());
        }
        public long nextLong(){
            return Long.parseLong(next());
        }
        public double nextDouble(){
            return Double.parseDouble(next());
        }
        public String next(){
            while(st == null || !st.hasMoreTokens()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch(IOException e){}
            }
            return st.nextToken();
        }
    }
}

why
solution of N = 8
is not 2 * 4
Instead it 1 * 8

8 Likes

2*4 is also correct answer. Will accept multiple correct answers.

class Codechef

{
public static void main(String[] args) throws NumberFormatException, IOException {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
PrintWriter pw = new PrintWriter(System.out);
int input = Integer.parseInt(br.readLine());
for (int i = 0; i < input; i++) {
int number = Integer.parseInt(br.readLine());
pw.print(1 + " " + number);
}
pw.flush();
}
}

Can anybody help me explaining why this code is not working for this problem? @smjleo @daanish_adm

I did consider the simplest solution. But to test myself, I did try decrementing from the square root of the input integer to find some other multiples. Despite the time complexity being sqrt(n), I receive a time limit exceeded status. Any help in this regard? @smjleo @daanish_adm

Here, I have attached my code snippet.

   int t, n;
   cin >> t; 
   while(t--)
    	{
    	    cin >> n;
    	    
    	    for(int i = (int)sqrt(n); i > 0; i--)
    	        if(! (n%i))
    	        {
    	            cout << i << " " << n/i << endl;
    	            break;
    	        }
    	}

Just change the test case for 10. Please mention there 1 10 instead of 2 5, otherwise it will create confusion for that test case.

1 Like