QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#876129#7704. Plus Minus Four SquaresLaVuna47#AC ✓38ms3712kbC++202.1kb2025-01-30 17:31:182025-01-30 17:31:18

Judging History

This is the latest submission verdict.

  • [2025-01-30 17:31:18]
  • Judged
  • Verdict: AC
  • Time: 38ms
  • Memory: 3712kb
  • [2025-01-30 17:31:18]
  • Submitted

answer

//A tree without skin will surely die.
//A man without face is invincible.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(S) ((int)S.size())
#define FOR(i, st_, n) for(int i = st_; i < n; ++i)
#define RFOR(i, n, end_) for(int i = (n)-1; i >= end_; --i)
#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef unsigned long long ull;
typedef long double LD;
typedef pair<ull, ull> pull;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using namespace std;
#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

const ll MAX_SQ = 72;


// 36 36 -4 -4
vector<ll> nums;

bool ok()
{
	FOR(i,1,4)
	{
		if(abs(nums[i]) == abs(nums[i-1]) && nums[i] != nums[i-1])
			return false;
	}
	return nums[0] >= 0;
}

ll f(ll num, ll prev, int k)
{
	if(k==0)
	{
		if(num==0 && ok())
		{
			//for(auto item: nums)
				//cout<<item<<" ";
			//cout<<'\n';
			return 1;
		}
		return 0;
	}
	ll res=0;
	// add;
	for(ll i = 0; i*i <= abs(prev); ++i)
	{
		nums.pb({i*i});
		res += f(num-i*i, i*i,k-1);
		nums.pop_back();
	}
	for(ll i = 1; i*i <= abs(prev); ++i)
	{
		nums.pb({-i*i});
		res += f(num+i*i, i*i,k-1);
		nums.pop_back();

	}

	return res;
}

int solve()
{
	ll n;
	if(!(cin>>n))return 1;
	cout<<f(n,n,4)<<'\n';
    return 0;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int TET = 1e9;
    //cin >> TET;
    for (int i = 1; i <= TET; i++)
    {
        if (solve())
        {
            break;
        }
#ifdef ONPC
        cout << "__________________________" << endl;
#endif
    }
#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3712kb

input:

64

output:

12

result:

ok single line: '12'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

65

output:

10

result:

ok single line: '10'

Test #3:

score: 0
Accepted
time: 7ms
memory: 3712kb

input:

2023

output:

245

result:

ok single line: '245'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

0

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 38ms
memory: 3584kb

input:

5000

output:

951

result:

ok single line: '951'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3584kb

input:

1024

output:

182

result:

ok single line: '182'

Test #7:

score: 0
Accepted
time: 7ms
memory: 3584kb

input:

2048

output:

355

result:

ok single line: '355'

Test #8:

score: 0
Accepted
time: 28ms
memory: 3712kb

input:

4096

output:

708

result:

ok single line: '708'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

1

output:

1

result:

ok single line: '1'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

2

output:

1

result:

ok single line: '1'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

4

output:

2

result:

ok single line: '2'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

3

output:

1

result:

ok single line: '1'

Test #13:

score: 0
Accepted
time: 2ms
memory: 3712kb

input:

1111

output:

131

result:

ok single line: '131'