题目链接:Problem - 1916D - Codeforces
打表看下有满足答案的组成集合
打表程序如下:
//
// Created by Botter on 2024/5/11.
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int w(ll x){
int cnt =0;
while (x){
cnt++;
x/=10;
}
return cnt;
}
int main() {
map<int,vector<ll>>m;
for (ll i = 1 ; ; i++){
if (i*i >= INT_MAX-1) break;
m[w(i*i)].push_back(i*i);
}
int t,n,ans=0;
cin >>t;
map<set<int>,int>s;
while(t--){
cin >>n;
set<int>a;
for (int i = 0;i<m[n].size();i++){
int t = m[n][i];
while(t){
a.insert(t%10);t/=10;
}
ans=max(ans,++s[a]);a.clear();
}
cout << ans<<endl;
for (auto [_,v]:s){
if (s[_] >= n) {
for (auto c: _) {
cout << c;
}
cout << " " << s[_] << endl;
}
}
}
return 0;
}
思路即实现:
通过输出,找了找,满足答案的集合里都有这样这个集合{0,1,6,9}。也就是说那么答案一定是由这个集合组成的,即若干0和1,6,9组成的数。我们还可以发现169,196,以及961都是可以平方数,那么我们在末尾添加n-3个0那么也一定是平方数,因为n是奇数。那么我们试试在中间添加相同个数零试试,不难发现10..60..9 一定是可以被10.. ..3平方的。196和961同理;我们前面输出了3种,可以添加0的情况一定有[0,],事实上已经满足n种了。枚举169和961两种
就可以了。
代码实现:
//
// Created by Botter on 2024/5/11.
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void slove(){
int n;
cin >> n;
if (n==1) {
cout << 1<<endl;
return;
}
//全补零的三个数(3)
cout << "169";
for (int i = 4 ; i <= n ; i++) cout << "0";
cout << endl;
cout << "196";
for (int i = 4 ; i <= n ; i++) cout << "0";
cout << endl;
cout << "961";
for (int i = 4 ; i <= n ; i++) cout << "0";
cout << endl;
for(int i = 1 ; i < n/2;i++){
cout << 1;
for (int j = 1;j <= i ; j++) cout << 0;
cout << 6;
for (int j = 1 ; j <= i ; j++) cout << 0;
cout << 9;
for (int j = 3+2*i;j<n;j++)cout << 0;
cout << endl;
cout << 9;
for (int j = 1;j<=i;j++) cout << 0;
cout <<6;
for (int j = 1;j<=i;j++) cout <<0;
cout << 1;
for (int j=3+2*i;j<n;j++) cout << 0;
cout << endl;
}
}
bool isovle(int x){
return sqrt(x)==int(sqrt(x));
}
int main() {
int t,n,ans=0;
cin >>t;
while(t--){
slove();
}
// cout << isovle(169)<<isovle(196)<<isovle(961);
return 0;
}