cf ed#70 D

The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

You are given an integer n.

You have to find a sequence s consisting of digits {1,3,7} such that it has exactly n subsequences equal to 1337.

For example, sequence 337133377 has 6 subsequences equal to 1337:

3371–33–3–77– (you can remove the second and fifth characters);
3371–3–33–77– (you can remove the third and fifth characters);
3371–3–3–377– (you can remove the fourth and fifth characters);
3371–33–3–7–7 (you can remove the second and sixth characters);
3371–3–33–7–7 (you can remove the third and sixth characters);
3371–3–3–37–7 (you can remove the fourth and sixth characters).
Note that the length of the sequence s must not exceed 105.

You have to answer t independent queries.

Input
The first line contains one integer t (1≤t≤10) — the number of queries.

Next t lines contains a description of queries: the i-th line contains one integer ni (1≤ni≤109).

Output
For the i-th query print one string si (1≤|si|≤105) consisting of digits {1,3,7}. String si must have exactly ni subsequences 1337. If there are multiple such strings, print any of them.

Example
Input
2
6
1
Output
113337
1337

将答案分解成133 7777(若干个7)33333(若干个3) 7 33333(若干个3)7
第一个1和最后一个7加上中间所有的3(假设有i1个3)组成的1337个数为i1*(i1-1)/2;
第一个1和中间的7加上两者之间的3(假设有i2个3)组成的1337个数为i2*(i2-1)/2;
剩下再用 n-i1*(i1-1)/2-i2*(i2-1)/2 个7放在最开始的133后面解决剩下的次数。

#include<stdio.h>
#include<math.h>
int n,t;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int i1;
		int maxn=1+sqrt(2*n);
		int tmp;
		for(i1=maxn;i1>1;i1--)
		{
			tmp=i1*(i1-1)/2;
			if(n>=tmp)
			break;
		}
		if(i1>1)
		n-=i1*(i1-1)/2;
		
		int i2;
		maxn=1+sqrt(2*n);
		for(i2=maxn;i2>1;i2--)
		{
			tmp=i2*(i2-1)/2;
			if(n>=tmp)
			break;
		}
		if(i2>1)
		n-=i2*(i2-1)/2;
		
		printf("133");
		for(int i=1;i<=n;i++)
		printf("7");
		
		if(i2>1)
		{
			for(int i=1;i<=i2-2;i++)
			printf("3");
			printf("7");
		}
		
		for(int i=1;i<=i1-i2&&i<=i1-2;i++)
		printf("3");
		printf("7\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值