ARC 127

A - Leading 1s
题意:设 \(f(x)\)\(x\) 这个数的前导 1 的个数。给定一个数 \(n (1<=n<=10^{15})\)
思路:枚举前导1的个数x,每次计算第x个前导1的贡献,因为只有前x-1个都是1,第x个1才有贡献。计算的时候逐渐增大,一个以最短的速度增大,一个以最大的速度增大,最大的要和n取一个min,然后每次的差值+1就是所有第x个前导1的贡献
jlscode:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ll n;
	cin >> n;
	ll ans = 0;
	for(int x = 1; x <= 16; ++x)
	{
		ll w = 0;
		for(int j = 0; j < x; ++j)
			w = w * 10 + 1;
		ll p = w;
		for(int y = x; y <= 16; ++y)
		{
			if(w <= n)
			{
				ans += min(n, p) - w + 1;
			}
			w *= 10;
			p = 10 * p + 9;
		}
	}
	cout << ans << endl;
	return 0;
}

B - Ternary Strings
题意:给定一个 \(n\)\(l\),构造\(3n\)个长度为\(l\)字符串,每个字符可以为\(0,1,2\).构造的3n个字符串中字典序最大的字符串,必须是所有答案中字典序最大的中最小的一个。
思路:
首先我们需要构造出
jlscode:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	int n, l;
	cin >> n >> l;
	for(int d = 0; d < 3; ++d)
		for(int i = 0; i < n; ++i)
		{
			string s;
			int x = i;
			for(int j = 1; j < l; ++j)
			{
				s += '0' + (x + d) % 3;
				x /= 3;
			}
			s += '0' + (2 + d) % 3;
			reverse(s.begin(), s.end());
			cout << s << "\n";
		}
	return 0;
}
posted @ 2021-09-26 08:49  ___Rain  阅读(154)  评论(0)    收藏  举报