codeforces-1202D-Print a 1337-string

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

题目链接: codeforces-1202D.

题目描述

           \;\;\;\;\; 构造一个数字序列 s s s 包含 3 3 3 种数字 ‘ 1 ’ ‘ 3 ’ ‘ 7 ’ ‘1’‘3’‘7’ 137,使其恰有 n n n " 1337 " "1337" "1337" 的子串。
           \;\;\;\;\; 问:输出任一满足要求的数字序列,长度不超过 1 0 5 10^{5} 105.
           \;\;\;\;\; 1 ≤ n ≤ 1 0 9 1\leq n \leq 10^{9} 1n109.

(题解)思路

&ThickSpace;&ThickSpace;&ThickSpace; \;\;\; 如果序列的形式为 133...337 133...337 133...337 1 1 1 7 7 7 之间有 x x x 3 3 3,那么子串 &quot; 1337 &quot; &quot;1337&quot; "1337" 的数量为 x ∗ ( x − 1 ) 2 \frac{x*(x-1)}{2} 2x(x1). 找到一个最大的 x x x 使 x ∗ ( x − 1 ) 2 &lt; n \frac{x*(x-1)}{2} &lt; n 2x(x1)<n,可以算出 x &lt; 45000 x &lt; 45000 x<45000. 还需构造 n − x ∗ ( x − 1 ) 2 n - \frac{x*(x-1)}{2} n2x(x1) 个子串,只要在第二个 3 3 3 之后插入 n − x ∗ ( x − 1 ) 2 n - \frac{x*(x-1)}{2} n2x(x1) 7 7 7 即可,可以算得 n − x ∗ ( x − 1 ) 2 &lt; 45000 n - \frac{x*(x-1)}{2} &lt; 45000 n2x(x1)<45000.

&ThickSpace;&ThickSpace;&ThickSpace; \;\;\; 时间复杂度:预处理 O ( x ) O(x) O(x), 计算 O ( 1 ) O(1) O(1),输出 O ( l e n ) O(len) O(len).

代码

/*
nero
2019-8-21 09:59:39 
*/

#include <map> 
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define lson(u) (u<<1)
#define rson(u) (u<<1|1)
#define Pi acos(-1)
#define iINF 0x3f3f3f3f
#define lINF 0x3f3f3f3f3f3f3f
#define EPS 0.000000001
#define pii pair<int,int>
typedef long long ll;
typedef unsigned long long ull;

int Max = 0;
ll c[50000];
int main()
{
	int T;
	ll n;
	for(ll i = 2; ;i++) {
		c[i] = i * (i-1) / 2; 
		if(c[i] >= (1e9)) {
			Max = i;
			break;
		}
	}	
	scanf("%d", &T);
	while(T--) {
		scanf("%lld", &n);
		int t = upper_bound(c+1, c+ Max + 1, n) - c;
		ll x = 1, y = t-1, z = n - c[t-1];		
		printf("133");
		for(int i = 1; i <= z; i++) printf("7");
		for(int i = 1; i <= y-2; i++) printf("3");
		printf("7\n");
	}	
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值