Codeforces Round 976 (Div. 2) and Divide By Zero 9.0题解记录(A,B,C)
A. Find Minimum Operations
暴力枚举
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<deque>
#include<math.h>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<random>
using namespace std;
typedef long long ll;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll ksm(ll x,ll y)
{
ll ans=1;
while(y)
{
if(y&1)
ans*=x;
x*=x;
y>>=1;
}
return ans;
}
ll gcd(ll x,ll y)
{
if(y==0)
return x;
else
return gcd(y,x%y);
}
int main()
{
fio();
ll t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
if(k==1)
{
cout<<n<<endl;
continue;
}
else
{
ll ans=0;
while(n)
{
if(n<k)
{
ans+=n;
break;
}
else
{
ll cnt=1;
for(ll i=1;i<=60;i++)
{
cnt*=k;
if(cnt>n)
{
cnt/=k;
break;
}
}
n-=cnt;
ans++;
}
}
cout<<ans<<endl;
}
}
}
B. Brightness Begins
通过分析可得答案使ans-sqrt(ans)>=n的最小ans值
这里要开srtl,否则怎么写都最多WA8
怎么说,一个sqrtl毁了我的梦(不是)
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<deque>
#include<math.h>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<random>
using namespace std;
typedef unsigned long long ll;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll ksm(ll x,ll y)
{
ll ans=1;
while(y)
{
if(y&1)
ans*=x;
x*=x;
y>>=1;
}
return ans;
}
ll gcd(ll x,ll y)
{
if(y==0)
return x;
else
return gcd(y,x%y);
}
int main()
{
fio();
ll t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n;
ll l=1,r=1844674407370955161;
while(l<r)
{
ll mid=(l+r)>>1;
ll j=sqrtl(mid);
if(mid-j>=n)
{
r=mid;
}
else
l=mid+1;
}
cout<<r<<endl;
}
}
C. Bitwise Balancing
直接分析b,c,d对应位置地二进制关系,总共8种可能
随后直接走一遍二进制所有位即可,记得答案能小就尽量小
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<deque>
#include<math.h>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<random>
using namespace std;
typedef long long ll;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll ksm(ll x, ll y)
{
ll ans = 1;
while (y)
{
if (y & 1)
ans *= x;
x *= x;
y >>= 1;
}
return ans;
}
ll gcd(ll x, ll y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
int main()
{
fio();
ll t;
cin >> t;
while (t--)
{
ll b, c, d;
cin >> b >> c >> d;
ll ans = 0;
ll cnt = 1;
ll g = 0;
while (b||c)
{
if (cnt > (ll)(1e18))
break;
ll e1 = (b & cnt);
ll e2 = (c & cnt);
if (e1)
b -= cnt;
if (e2)
c -= cnt;
ll op = (d & cnt);
if (e1)
{
if (e2)
{
if (op)
{
d -= cnt;
}
else
{
ans += cnt;
}
}
else
{
if (op == 0)
{
g = 1;
}
else
{
d -= cnt;
}
}
}
else
{
if (e2 == 0)
{
if (op)
{
ans += cnt;
d -= cnt;
}
}
else
{
if (op)
{
g = 1;
}
}
}
cnt *= 2;
}
if (d > 0)
{
ll cnt = 1;
for (ll i = 0; i <= 62; i++)
{
if (d & cnt)
{
d -= cnt;
ans += cnt;
}
if (d == 0)
break;
cnt *= 2;
}
}
ll a = ksm(2, 61);
//cout << a << endl;
if (g==0&&ans<=a)
{
cout << ans << endl;
}
else
cout << -1 << endl;
}
}