AtCoder Beginner Contest 251

比赛链接

AtCoder Beginner Contest 251

D - At Most 3 (Contestant ver.)

Problem Statement

You are given an integer \(W\).
You are going to prepare some weights so that all of the conditions below are satisfied.

  • The number of weights is between 1 and 300 , inclusive.
  • Each weight has a mass of positive integer not exceeding \(10^{6}\).
  • Every integer between 1 and \(W\), inclusive, is a good integer. Here, a positive integer \(n\) is said to be a good integer if the following condition is satisfied:
    • We can choose at most 3 different weights from the prepared weights with a total mass of \(n\).

Print a combination of weights that satisfies the conditions.

Constraints

  • \(1 \leq W \leq 10^{6}\)
  • \(W\) is an integer.

解题思路

思维,构造

构造这样一些数:
\(99\) 个:\(1,2,3,\dots,99\) \(\ (1)\)
\(99\) 个:\(100,200,300,\dots ,9900\) \(\ (2)\)
\(99\) 个:\(10000,20000,30000,\dots,990000\) \(\ (2)\)
\(297\) 个数
显然,一和二位的数已经存在,对于三位的数,可用 \((1)\)\((2)\) 组合,故可用 \(2\) 个数得到三位数,此时存在的四位数为 \(1000,1100,1200,\dots ,9900\),可以 \((1)\) 与这些数组合得到任意四位数,即可用 \(2\) 个数得到四位数,此时存在的五位数为 \(10000,20000,30000,\dots ,\90000\),可用四位数与其组合成任意五位数,即可用 \(3\) 个数得到五位数,另外,\(1000000\) 可用 \(990000\)\(10000\) 得到

  • 时间复杂度:\(O(1)\)

代码

// Problem: D - At Most 3 (Contestant ver.)
// Contest: AtCoder - Panasonic Programming Contest 2022(AtCoder Beginner Contest 251)
// URL: https://atcoder.jp/contests/abc251/tasks/abc251_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

int main()
{
	int w;
	cin>>w;
    cout<<297<<'\n';
    for(int i=1;i<=99;i++)cout<<i<<' '<<i*100<<' '<<i*10000<<' ';
    return 0;
}
posted @ 2022-05-14 22:04  zyy2001  阅读(137)  评论(0编辑  收藏  举报