codeforces1202D

Title description

Insert picture description here
Insert picture description here

General idea

Given n (n≤10 9 ), find a string S that satisfies
①|S|≤10 5
②Si∈['1','3','7']
③The number of subsequence "1337" in S is just right N

answer


Obviously, it is not straightforward to construct problems of human wisdom. 11111…11337
Since 3 grows by the square level, first add a large number of 3 to make the answer close to n, and then add 7 to make up for n.
Consider a construction method:
133777…7 (a 7) 333…3 (b 3) 7,
then sum=(a+1)+(b*(b-1)/2+2*b)
Obviously the left and right parts do not affect each other, and the right side is based on the square level Increase
so violently enumerate the largest possible b, and then subtract to get a

code

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std;

int T,n,i,j,k,l,a,b;

int main()
{
    
    
//	freopen("CF1202D.in","r",stdin);
//	freopen("CF1202D.out","w",stdout);
	
	scanf("%d",&T);
	for (;T;--T)
	{
    
    
		scanf("%d",&n);
		--n;
		
//		133777..7(a)333...3(b)7 ans=(a+1)+(b(b-1)/2+2b)
		
		fo(b,1,n)
		if ((b*(b-1)/2+2*b)>n)
		break;
		--b;
		a=n-(b*(b-1)/2+2*b);
		
		printf("133");
		fo(i,1,a)
		printf("7");
		fo(i,1,b)
		printf("3");
		printf("7");
		printf("\n");
	}
}

Guess you like

Origin blog.csdn.net/gmh77/article/details/98933440