C++ 2020B — Brightness Begins

codeforces原题

洛谷翻译

#include <bits/stdc++.h>
#if __cplusplus < 201103L
	#error "should use c++11 implementation"
#endif
namespace fast_IO { template <typename T> inline void write(T x) { if ( x < 0 ) putchar('-'), x = -x; if ( x == 0 ) { putchar('0'); return; } if ( x > 9 ) write(x/10); putchar(x % 10 + '0'); return; }
	template <typename T> inline void read(T &x) { x = 0; int w = 1; char ch = getchar(); while ( !isdigit(ch) ) { if ( ch == '-' ) w = -1; ch = getchar(); } while ( isdigit(ch) ) x = (x<<1) + (x<<3) + (ch^48), ch = getchar(); x *= w; return; } }
using namespace fast_IO;
using namespace std;
using ull = unsigned long long;
using ll = long long;
#define endl '\n'
#define il inline
#define re register
#define int long long
#define writes(x) write(x),putchar(' ')
#define writel(x) write(x),putchar('\n')
#define IOS_NULL ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)

const int INF_MX = 0x3f3f3f3f; //  1061109567
const int INF_MN = 0xc0c0c0c0; // -1061109568

void solve()
{
	/* 有偶数因数的灯就保持 on */
	/* 有奇数因数(完全平方数)的灯就变为 off */
	/* 相当于寻找第 k 个完全平方数 */
	/* n个数中,一共有 sqrt(n) 个完全平方数 (off) */
	/* n个数中,一共有 n-sqrt(n) 个非完全平方数 (on) */
	auto get_sqrt = [&](ll x) {
		ll l = 0, r = 2e9;
		while (l < r) {
			ll mid = l + r + 1 >> 1;
			if (mid * mid <= x) {
				l = mid;
			}
			else {
				r = mid -1;
			}
		}
		return l;
	};
	ll k;
	cin >> k;
	ll l = 1, r = 2e18;
	while (l < r) {
		ll mid = l + r >> 1;
		if (mid - get_sqrt(mid) < k) {
			l = mid + 1;
		} 
		else {
			r = mid;
		}
	}
	cout << l << '\n';
}

signed main()
{
	IOS_NULL;
	int t; cin >> t;
	while (t--) solve();
	return 0;
}

最优解

#include <bits/stdc++.h>
#if __cplusplus < 201103L
	#error "should use c++11 implementation"
#endif
using namespace std;
using ull = unsigned long long;
using ll = long long;
#define endl '\n'
#define il inline
#define re register
#define int long long
#define writes(x) write(x),putchar(' ')
#define writel(x) write(x),putchar('\n')
#define IOS_NULL ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)

const int INF_MX = 0x3f3f3f3f; //  1061109567
const int INF_MN = 0xc0c0c0c0; // -1061109568

void solve()
{
	ll k; cin >> k;
	cout << k + (ll)(sqrtl(k)+0.5) << '\n';
}

signed main()
{
	IOS_NULL;
	int t; cin >> t;
	while (t--) solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值