D. Print a 1337-string...(分析+构造)

题目

题意:

    要求构造一个字符串,使得子串为1337的个数恰好为n。字符串长度不能超过1e5。
     1 n 1 0 9 1≤n≤10^9

分析:

    我们构造分为三个部分,显然3的数量越多对答案的贡献越大。所以先二分出3的个数x,应该是满足x*(x-1)/2<=n的最大的x。然后剩余rest的就在133后面跟上7,这个7对答案的贡献为1。所以构造为133+rest个7+(x-2)个3+7。

#include <iostream>
using namespace std;

typedef long long ll;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while( t-- )
	{
		ll n;
		cin >> n;
		int l = 2,r = 1e5;
		int ans = 0; 
		while( l <= r )
		{
			int mid = ( l + r ) / 2;
			if( (ll)mid*(mid-1) <= 2 * n )
			{
				ans = mid;
				l = mid + 1;
			}else r = mid - 1;
		}
		int gap = n - ans * (ans-1) / 2;
		cout << "133";
		for (int i = 1; i <= gap; i++) cout << '7';
		for (int i = 3; i <= ans; i++) cout << '3';
		cout << '7' << '\n'; 
	}
	return 0;
}

发布了132 篇原创文章 · 获赞 6 · 访问量 7919

猜你喜欢

转载自blog.csdn.net/weixin_44316314/article/details/105012902