[CF] C. Make a Square - Educational Codeforces Round 42 (Rated for Div. 2)

https://codeforces.com/contest/962/problem/C

题目大意

给出数字 $n \ (1 \le n \le 2 \times 10^9)$。问能否从 $n$ 中删掉一些数字得到另一个不含前导零的数字(循序不变),使得新数字是一个平方数。不能输出 $-1$,能则输出最少需要删除的数。

简要题解

这里注意到一个隐含的条件是这个数字位数不超过 $10$。这样我们直接枚举所有删数可能即可。我们可以 $O(1)$ 的判断,或 $sqrt(n)$ 的预处理比 $n$ 小的所有平方数。

复杂度

$T$:$O(\log_{10} n)$

$S$:$O(\log_{10} n)$

代码实现

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

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n; cin >> n;

  vector<int> w;
  while (n) {
    w.push_back(n % 10);
    n /= 10;
  }
  reverse(w.begin(), w.end());

  int m = w.size();
  int msk = 1 << m;

  int ans = m + 1;

  for (int i = 0; i < msk - 1; i++) {
    int a = 0;
    int fi = 1;
    for (int j = 0; j < m; j++) {
      if (i & (1 << j)) continue;

      if (fi) {
        if (w[j] == 0) break;
        fi = 0;
      } 
      a = a * 10 + w[j];
    }
    if (fi) continue;

    int b = sqrt(a);

    if (b * b == a) {
      cmin(ans, (int)__builtin_popcount(i));
    }
  }

  if (ans == m + 1) ans = -1;
  cout << ans << '\n';
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

复杂度

$T$:$O(\sqrt{n} \log \sqrt{n})$

$S$:$O(\sqrt(n))$

代码实现

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

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n; cin >> n;
  set<int> sqr;
  for (int i = 0; i * i <= n; i++) {
    sqr.insert(i * i);
  }

  vector<int> w;
  while (n) {
    w.push_back(n % 10);
    n /= 10;
  }
  reverse(w.begin(), w.end());

  int m = w.size();
  int msk = 1 << m;

  int ans = m + 1;

  for (int i = 0; i < msk - 1; i++) {
    int a = 0;
    int fi = 1;
    for (int j = 0; j < m; j++) {
      if (i & (1 << j)) continue;

      if (fi) {
        if (w[j] == 0) break;
        fi = 0;
      } 
      a = a * 10 + w[j];
    }
    if (fi) continue;

    if (sqr.count(a)) {
      cmin(ans, (int)__builtin_popcount(i));
    }
  }

  if (ans == m + 1) ans = -1;
  cout << ans << '\n';
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] B. Students in Railway Carriage - Educational Codeforces Round 42 (Rated for Div. 2)
Next: [CF] D. Merge Equals - Educational Codeforces Round 42 (Rated for Div. 2)