ABC-400-C - 2^a b^2 (使用sqrtl) · znzryb
## 思路讲解
赛时想到思路了,也写出来了,但是死活都会WA四个点。
这种问题一般就是转化成二进制想,其实2^a就是二进制下右移a位。
最后靠AI手写了个二分的开方程序过了,这个是在有点离谱,现在在想我的程序为什么会WA。
找到一个博客,说sqrt是有点问题。
[https://codeforces.com/blog/entry/107717](https://codeforces.com/blog/entry/107717)
利用sqrtl就过了,离谱,floor(sqrt(i))就不行。
其实sqrtl其实是传入参数为long double,那么其实还是精度问题。我直接这么调用其实还有一个自动向下的隐式转换。
[https://en.cppreference.com/w/c/numeric/math/sqrt](https://en.cppreference.com/w/c/numeric/math/sqrt)
## AC代码
[https://atcoder.jp/contests/abc400/submissions/64576953](https://atcoder.jp/contests/abc400/submissions/64576953)
::: 源代码
```cpp
// Problem: C - 2^a b^2
// Contest: AtCoder - AtCoder Beginner Contest 400
// URL: https://atcoder.jp/contests/abc400/tasks/abc400_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
typedef pair<ll,bool> plb;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(5e18)+3;
ll N,M,T,A[MAXN];
inline void solve(){
cin>>N;
// ll cnt=0;
// for(ll i=N;i>0;i/=2){
// ++cnt;
// }
// #ifdef LOCAL
// cerr << cnt << '\n';
// #endif
ll ans=0;
// ll cnt=0;
// ll upRes=0;
for(ll i=N>>1;i>0;i>>=1){
ll res=sqrtl(i); //-floor(sqrt(i>>1));
res=ceil(res*1.0l/2);
// ++cnt;
// #ifdef LOCAL
// cerr<<i<<" " << res << '\n';
// #endif
ans+=res; // *cnt
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>T;
// while(T--){
// solve();
// }
solve();
return 0;
}
/*
*/
```
:::
## 心路历程(WA,TLE,MLE……)
666