Task

mostbet kz на андроид скачать For every integer n (1 ≤ n ≤ 10⁶) we have to output the smallest positive integer k such that

k · n is a perfect square.

mostbet kz на андроид скачать In other words, we need the least multiplier that turns n into a square.

Observations

  • Let the prime factorisation of n be

n = p₁^e₁ · p₂^e₂ · … · p_m^e_m .

  • For a product to be a perfect square, every exponent must be even.
  • If a prime appears in n with an odd exponent, we must multiply by the
    same prime once more to make its exponent even.
  • Multiplying by a prime twice would increase the exponent by two,
    which is unnecessary when the current exponent is already even.

Hence

k = ∏(p_i  for all i with e_i odd)

i.e.the product of all distinct primes whose exponents in n are odd.

Algorithm

For each query n:

  1. ans = 1.
  2. For each prime p from the pre‑generated list while p·p ≤ n
    * count cnt – how many times p divides n.
    * if cnt is odd, multiply ans by p.
    * divide n by p until it is no longer divisible.
  3. If the remaining n is larger than 1, it is a prime with exponent 1
    → multiply ans by this n.
  4. astana.blizko.kz Output ans.

The sieve up to √10⁶ = 1000 gives all primes needed for factorisation.

Correctness Proof

We prove that the algorithm outputs the smallest integer k such that
k·n is a perfect square.

Lemma 1

Let p be a prime dividing n with exponent e.
If e is even, multiplying by p any number of times does not change
the parity of the exponent of p in k·n.
If e is odd, exactly one factor p must appear in k to make the
exponent of p in pthrup.com k·n even.

Proof.

In k·n, the exponent of p equals e + f, where f is the exponent
of p in k.
To be even, e + f must be even.
If e is even: e is already even, so f can be any integer (including 0)
and the parity stays even.
If e is odd: to obtain an even sum, f must be odd; the smallest
possible odd value is 1.∎

Lemma 2

Let P be the set of distinct primes whose exponents in n are odd.
Then k = ∏_p∈P p makes k·n a perfect square.

Proof.

For any prime q not in P, its exponent in n is even,
hence after multiplying by k the exponent remains even
(Lemma 1).
For any prime p in P, its exponent in n is odd and
k contributes exactly one more factor of p,
so the new exponent is odd + 1 = even.
All primes now have even exponents; therefore k·n is a perfect square.∎

Lemma 3

Any integer k' that makes k'·n a perfect square must contain
every prime in P at least once.

Proof.

Assume a prime p∈P does not divide k'.
Then the exponent of p in k'·n equals the odd exponent of p in n,
which is odd, contradicting that k'·n is a perfect square.∎

Lemma 4

Among all integers satisfying Lemma 3, the product k = ∏_p∈P p
is the smallest.

Proof.

Every valid k' must contain each prime of P at least once
(Lemma 3).
Therefore k' is a multiple of k.
Since k itself satisfies the condition (Lemma 2), it is the minimal
such integer.∎

Theorem

For every input n, the algorithm outputs the smallest positive integer
k such that k·n is a perfect square.

Proof.

During factorisation the algorithm multiplies ans by a prime p
iff the exponent of p in n is odd.
Consequently, after the loop ans equals ∏_p∈P p,
exactly the integer described in Lemma 4.
By that lemma this ans is the minimal multiplier making n
a perfect square.∎

Complexity Analysis

Let π(x) be the number of primes not exceeding x.

Precomputation:
Sieve up to 1000O(1000 log log 1000) time, negligible memory.

Per query:
Trial division over all primes ≤ √n.
Worst case π(1000) ≈ 168.
Each division reduces n; the number of iterations is bounded by
O(log n) per prime.

Hence for each query the running time is O(π(√n)) ≈ O(200),
and for Q queries total time is O(Q · 200) – easily fast for
Q ≤ 10⁵.
Memory usage is O(π(1000)) for the prime table.

Reference Implementation (GNU‑C++17)

#include <bits/stdc++.h>
using namespace std;

/* ---------- Sieve of Eratosthenes up to 1000 ---------- */
static vector<int> primes;
void build_primes() 
  const int LIMIT = 1000;     // sqrt(1e6)
  vector<bool> is_prime(LIMIT + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i * i <= LIMIT; ++i)
    if (is_prime[i])
      for (int j = i * i; j <= LIMIT; j += i)
        is_prime[j] = false;
  for (int i = 2; i <= LIMIT; ++i)
    if (is_prime[i]) primes.push_back(i);


/* -------------------- Main --------------------------- */
int main() 
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  build_primes();

  int Q;           // number of queries
  if (!(cin >> Q)) return 0;
  while (Q--) 
    long long n;      // n fits into 64 bit
    cin >> n;

    long long ans = 1;
    long long tmp = n;

    for (int p : primes) 
      if (1LL * p * p > tmp) break;
      int cnt = 0;
      while (tmp% p == 0) 
        tmp /= p;
        ++cnt;
      
      if (cnt & 1) ans *= p;  // odd exponent → multiply once
    

    if (tmp > 1)         // remaining prime factor
      ans *= tmp;         // its exponent is 1 (odd)
    

    cout << ans << '\n';
  
  return 0;

The program follows exactly the algorithm proven correct above
and conforms to the GNU++17 compiler.

Scroll to Top