// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lld long double
#define w(x) ll x;cin>>x;while(x--)
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) __detail::__lcm(a,b)
#define goog(tno) cout << "Case #" << tno <<": "
#define PRESS_F_TO_PAY_RESPECT ios_base::sync_with_stdio(false), cin.tie(nullptr)
using namespace std;
ll dx[]= {-1,-1,-1,0,0,1,1,1};
ll dy[]= {-1,0,1,-1,1,-1,0,1};
const lld pi=3.1415926535897932384626433832795;
const ll INF=1e18;
const ll mod=1000000007;
const ll maxn=1e5+5;
// a^2=2*b+1;
// c=b+1;
int main(){
PRESS_F_TO_PAY_RESPECT;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
w(T){
ll n; cin>>n;
ll ans=0;
for(ll a=2;a*a<=2*n+1;a++){
ll b=((a*a)-1)/2;
ll c=b+1;
if(a*a+b*b!=c*c) continue;
if(c!=a*a-b) continue;
if(a>=1&&a<=n&&b>=1&&b<=n&&c>=1&&c<=n){
ans++;
}
}
cout<<ans<<'\n';
}
return 0;
}
Hey there, can anyone tell me why first code got accepted using C++17(64) but not with C++17 ?
Dude, just link the two relevant submissions. Or at the very least, be clear that this is about TLE in
C++17(32 bit)
and AC inC++17(64 bit)
.About why this happened. You are using long longs (64 bit integers) in your code. Operations on 64 bit integers simply run much faster on
C++17(64 bit)
thanC++17(32 bit)
.The 32 bit version is really just a relic from the past. Don't use it.
First, you should read this article. You can understand, "C++17" is compiled on 32bit system and "C++17(64)" is compiled on 64bit system.
In 32bit system's register only have 32bit, so, long long(=64bit) operation (+ or — or some operation) need 2 instructions(= CPU command). However, 64bit system's can it only 1 instruction.
Your code needs many operation to long long in 32bit system(C++17). so TLE, I think.
Test:
Let's test the code in "Custom Test".
Because, all "+" opertion needs 2 instrinction in C++17.
And then let's change from
long long a, b, c=0;
toint a, b, c=0;
. (Yes, this code will be overflow int, but it is not effect to execute time.) Then, result areBecause, all operation did by 1 instrction.