/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 130ms 1.68 MiB
#2 Accepted 131ms 1.676 MiB
#3 Accepted 131ms 1.68 MiB
#4 Accepted 149ms 1.785 MiB
#5 Accepted 150ms 1.781 MiB

Code

#ifndef DEBUG_H
#define DEBUG_H 1
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#ifndef CLown1331
#define debug(...) 0
#define ASSERT(...) 0
#define dbg(...) 0
#endif
#endif // DEBUG_H
#ifndef solution_h
#define solution_h 1
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
namespace solution
{
using namespace std;
using ll = long long;
using ull = long long;
const int sz = 3e5 + 105;
const int mod = 1e9 + 7;
const ll max_possible = 1LL * sz * mod;
int t;
int n;
vector <int> perfect_squares;
int dp[sz];
void solve_case()
{
    scanf("%d", &n);
    printf("%d\n", dp[n] > n ? -1 : dp[n]);
}
void solve()
{
    for (int i = 1; i * i <= sz; i++)
    {
        perfect_squares.push_back(i * i);
    }
    dp[0] = 0;
    fill(dp + 1, dp + sz, mod);
    for (int j = 0; j < perfect_squares.size(); j++)
    {
        for (int i = sz - 1; i >= perfect_squares[j]; i--)
        {
            dp[i] = min(dp[i], dp[i - perfect_squares[j]] + 1);
        }
    }
    scanf("%d", &t);
    while (t--)
    {
        solve_case();
    }
}
} // namespace solution
#endif // solution_h
#define _CRT_SECURE_NO_WARNINGS
int main()
{
    solution::solve();
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1051 Square Sum
Language
C++20 (G++ 13.2.0)
Submit At
2024-05-05 22:42:23
Judged At
2024-11-11 03:35:39
Judged By
Score
100
Total Time
150ms
Peak Memory
1.785 MiB