Codeforces Round 976 (Div. 2) and Divide By Zero 9.0(A,B,C)

长皆發表於2024-09-30

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;
	}
}

相關文章