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

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

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

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

char “1” “3” “1” “337”
num 1 C w 2 C^{2}_w n - C w 2 C^{2}_w 1

就是这样

#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, #

猜你喜欢

转载自blog.csdn.net/weixin_43164778/article/details/98946440