D - Print a 1337-string... CodeForces - 1202D

#include <bits/stdc++.h>
using namespace std;
/*
给你一个数n,让你求一个串,里面的子序列“1337”的数量恰好为n
开始时用1的数量*C(m,2)*7的数量,不好处理
题解中用13377773333337的形式,使得C(m,2)+7的数量=n;
这样更好处理
*/
typedef long long ll;
const int maxn=1e9;
int a[100000];//记录C(i,2)
int cnt;//纪律数组的长度
void init()
{
    for(int i=2; i<100000; i++)
    {
        if(i*(i-1)/2>maxn)
            {cnt=i;
             break;
            }
        a[i]=i*(i-1)/2;
    }
}
int main()
{
//    ll m=(ll)((ll)1<<31)-1;//必须把1弄成ll
    int t,n;
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%d",&n);
        int pos=upper_bound(a,a+cnt,n)-a-1;
        int y=n-a[pos];//7的个数
        printf("133");
        for(int i=0; i<y; i++)
            printf("7");
        for(int i=0; i<pos-2; i++)
            printf("3");
        printf("7\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zhangzhenjun/p/11702888.html