[ABC324D] Square Permutation

· · 题解

那么对于一个完全平方数 $x$,首先它的位数不能比给定的数多,但可以比给定的位数少(当然也可以相同),因为这是前导零在捣乱。然后判断是否存在一种原数的排列与 $x$ 相同。 完成这件事的经典做法就是将两个数按照数位排序,然后按位比较是否相同即可。 ```cpp int n, res, a[N], b[N]; int calc(int x) { int res = 0; while (x) { ++ res; x /= 10; } return res; } bool chk(int x) { for (int i = 1; i <= n; ++ i ) { b[i] = x % 10; x /= 10; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; ++ i ) if (a[i] != b[i]) return false; return true; } main() { n = read(); for (int i = 1; i <= n; ++ i ) { char c; cin >> c; a[i] = c - '0'; } if (n == 1 && !a[1]) return puts("1"), 0; sort(a + 1, a + n + 1); for (int i = 1; i <= 1e7; ++ i ) { int x = i * i, y = calc(x); if (y > n) break; res += chk(x); } wel(res); return 0; } ```