//fast
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(n) for(int i = 0 ; i<n ; i++)
#define all(x) x.begin(),x.end()
#define pb push_back
const int base = 5000;
vector<int> kw;
vector<pair<int,int>> pom;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 1 ; i<=base ; i++){
for (int j = i ; j<=base ; j++){
kw.pb(i*i+j*j);
}
}
sort(all(kw));
int last = -1;
for (int i : kw){
if (last==i){
pom.back().second++;
}else pom.pb({i,1});
last = i;
}
int n;
cin >> n;
ll w = 0;
for (int i = 1 ; i<=n ; i++){
for (int j = 1 ; j<=n ; j++){
int xd = i*i-j*j;
if (xd<=1) continue;
pair<int,int> xdd = {xd,0};
auto it = lower_bound(all(pom),xdd);
if (it!=pom.end() && (*it).first==xd){
w+=(*it).second;
}
}
}
cout << w << '\n';
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | //fast #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(n) for(int i = 0 ; i<n ; i++) #define all(x) x.begin(),x.end() #define pb push_back const int base = 5000; vector<int> kw; vector<pair<int,int>> pom; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); for (int i = 1 ; i<=base ; i++){ for (int j = i ; j<=base ; j++){ kw.pb(i*i+j*j); } } sort(all(kw)); int last = -1; for (int i : kw){ if (last==i){ pom.back().second++; }else pom.pb({i,1}); last = i; } int n; cin >> n; ll w = 0; for (int i = 1 ; i<=n ; i++){ for (int j = 1 ; j<=n ; j++){ int xd = i*i-j*j; if (xd<=1) continue; pair<int,int> xdd = {xd,0}; auto it = lower_bound(all(pom),xdd); if (it!=pom.end() && (*it).first==xd){ w+=(*it).second; } } } cout << w << '\n'; } |
English