codeforces D. Print a 1337-string...

题目链接:https://codeforc.es/contest/1202/problem/D
outputstandard output
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
inputCopy
2
6
1
outputCopy
113337
1337
一开始我用1和7来装,超时来;后来想用1和3来装但是时间不够了没想出来;今天补题的时候看到了别人的代码才找到怎么做;

  1. 先找到最多能要多少3,也就是C(n,2)小于等于n;
  2. 找到3的长度就可以找到1的长度了,就等于n-len3*(len3-1)/2;
    3.最后在末尾输出337;
    AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll fin(ll n){
    ll ans=2;
    while((ans+1)*(ans)/2<=n) ans++;
    return ans;
}
int main(){
    ll t;
    scanf("%lld",&t);
    while(t--){
        ll n;
        scanf("%lld",&n);
        ll len3=fin(n);
        ll len1=n-len3*(len3-1)/2;
        //cout<<len3<<"  "<<len1<<endl;
        printf("1");
        for(ll i=1;i<=len3-2;i++) printf("3");
        for(ll i=1;i<=len1;i++) printf("1");
        printf("337\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值