题目链接: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来装但是时间不够了没想出来;今天补题的时候看到了别人的代码才找到怎么做;
- 先找到最多能要多少3,也就是C(n,2)小于等于n;
- 找到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;
}