キーエンス プログラミング コンテスト 2020 C - Subarray Sum
備忘録
問題
回答
import sys import os import math import bisect import itertools import collections import heapq import queue import array import time # 時々使う # from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall # from decimal import Decimal # from collections import defaultdict, deque # 再帰の制限設定 sys.setrecursionlimit(10000000) def ii(): return int(sys.stdin.buffer.readline().rstrip()) def il(): return list(map(int, sys.stdin.buffer.readline().split())) def fl(): return list(map(float, sys.stdin.buffer.readline().split())) def iln(n): return [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)] def iss(): return sys.stdin.buffer.readline().decode().rstrip() def sl(): return list(map(str, sys.stdin.buffer.readline().decode().split())) def isn(n): return [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)] def lcm(x, y): return (x * y) // math.gcd(x, y) MOD = 10 ** 9 + 7 INF = float('inf') def main(): if os.getenv("LOCAL"): sys.stdin = open("input.txt", "r") N, K, S = il() ret = [S] * K + [MOD-8] * (N - K) print(*ret) if __name__ == '__main__': main()
考え方
(l, r)
の組はl <= r
、つまりl
とr
が同値であってもよいので愚直にS
を並べる。
- 回答概要
S
をK
個並べる- 残りの整数は足されたときに
S
とならない値で埋める
ある数列から左端と右端を決め、範囲内の合計値をS
とする問題です。
しかし、問題の制約からl <= r
であるため、l
とr
が同値でも問題ありません。
つまり、N = 3, K = 3, S = 1
のとき、[1, 1, 1]
のような数列が回答となります。
愚直にS
をK
個並べることによって、求められる数列を作ることが出来ます。
ただし、残りのN - K
個の整数に注意が必要です。
例えばN = 5, K = 2, S = 2
などのとき、残りのN - K
個の整数を1
で埋めると、
[2, 2, 1, 1, 1]
となり、(l, r) = (3, 4) = 2
となってしまい、
ちょうどS
となる組がK
個以上になります。
そのため、残りのN - K
個の整数については扱いに注意が必要です。
上記の回答例は一応ACだったけど、
S = 10 ** 9 - 1
のとき、不正解になるのでは。。。