【CodeForces】1202D. Print a 1337-string... 题解(数学组合问题)

31 篇文章 0 订阅
22 篇文章 0 订阅

题目来自于:CodeForces - 1202D
在这里插入图片描述在这里插入图片描述
写这题的时候感觉很开心,因为主要靠思路,不过看到代码过的很有技巧性,所以来补个题解
而且cf只有两个小时,太快乐了T_T

这题的思路是这样的,就是你输出的字符串长度是有限制的,在 |105 | 以内,所以我们不能将这个序列组合单单拆分成 “1” × n + “337” 这样的组合,会超出长度。

所以我们先在3下面下手,我们知道 C n 2 C^{2}_n Cn2的组合数结果就是n*(n-1)/2,长度大于 |105| 的字符串可以被拆开成以下的形式

char“1”“3”“1”“337”
num1 C w 2 C^{2}_w Cw2n - C w 2 C^{2}_w Cw21

就是这样

#include<bits/stdc++.h>
#define REP(i,s,t) for(int i=s;i<=t;i++)
using namespace std;
int main(){
	int T; cin>>T;
	while(T--){
		int n; cin>>n;
		int w;
		for(w=2;w*(w-1)/2<=n;w++);
		w--;
		cout<<1;
		REP(i,1,w-2) cout<<3;
		REP(i,1,n-w*(w-1)/2) cout<<1;
		cout<<"337"<<'\n';
	}
	return 0;
}//By couplefire, contest: Educational Codeforces Round 70 (Rated for Div. 2), problem: (D) Print a 1337-string..., Accepted, #
for _ in range(int(input())):
    n = int(input())
    if n <= 10 ** 5 - 5:
        print('133' + '7' * n)
    else:
        cf = 300
        g = n - (cf + 2) * (cf + 1) // 2
        b = g // (cf * (cf - 1) // 2)
        c = g % (cf * (cf - 1) // 2)
        print('1' + '3' * cf + '7' * b + '1' * c + '337')
# By Sevlll, contest: Educational Codeforces Round 70 (Rated for Div. 2), problem: (D) Print a 1337-string..., Accepted, #
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值