题解:AT_arc129_c [ARC129C] Multiple of 7
题目传送门
题目大意为:构造一个字符串
打个比方,有一个字符串
但是,这道题 如果有多个满足条件的答案,输出任意一个均可,所以,我们只用最简单的构造就行啦!那么,最简单的:既然是
比如上面那个例子,我们就用
接下来,就是找规律了:
相信大家不难发现,如果有
for(int i=ceill(sqrt(n*2));i>0;i--){
while(i*(i+1)/2<=n)q.push(i),n-=i*(i+1)/2;
//每个数有可能被多个i*(i+1)/2计算,所以这里是while而不是if,比如是5,很明显是1+1+3吧,1会被算2次
}
难道这道题这么简单,这是青色的吗?
可是,中间夹着的几个数没有要求吗?
这也正是这道题值这个颜色的原因。比较暴力的,就比如大佬 EuphoricStar 的做法,随机化这个值,然后检查是否合法,这之所以是对的,就是因为答案的
但我想讲的是,我们设一个区间
回到这道题,我们假设答案有两个被
需要注意这种方法仅限于你能用不超过
AC code
#include<bits/stdc++.h>
using namespace std;
int n;
int d[6] = {2, 3, 1, 5, 4, 6};//打表内容
int main() {
ios::sync_with_stdio (false);
cin.tie (nullptr);
cout.tie (nullptr);
// freopen("number.in","r",stdin);
// freopen("number.out","w",stdout);
cin >> n;
stack <int> q;
int tot = 0, ss = 0;
for (int i = ceill (sqrt (n * 2)); i > 0; i--) {
while (i * (i + 1) / 2 <= n) q.push(i), n -= i * (i + 1) / 2, tot++;
}
while (!q.empty()) {
int x = q.top();
q.pop();
ss += x;
for (int i = 1; i <= x; i++) cout << 7;
tot--;
if (tot) cout << d[ss % 6];
ss++;
//ss是当前位数,注意到这里是栈,所以是倒序的。
}
return 0;
//完美散花
}