蓝桥杯 阅兵方阵(预处理+暴力)

预处理一下平方数(还是第一次开2e8的数组),然后暴力判断一下就好,注意只需要枚举一半即可,例如13=4+9,如果枚举到9+4那么显然就重复了。

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e8 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
//int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool sq[maxn];
LL num[maxn];
bool check(LL val)
{
    int cnt = 0;
    int f = 0;
    for (int i = 1; i <= 10000; i++)
    {
        LL t = val - num[i];
        if (t < 0 || num[i] > t) //  只需要枚举前一半,继续枚举产生重复
            break;
        if (sq[t] == true)
        {
            cnt++;
            // cout << num[i] << " " << t << endl;
        }
    }
    return cnt == 12;
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    for (int i = 1; i <= 10000; i++)
    {
        // 预处理平方数字
        sq[i * i] = true;
        num[i] = LL(i) * LL(i);
    }
	for(LL i=1106;;i++)
	{
		if(check(i))
		{
			cout<<i;
			break;
		}
	}
    return 0;
}

答案是:160225

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/109106195