0
0

More than 3 years have passed since last update.

Atcoder ABC163 A-DをPythonで

Last updated at Posted at 2020-04-19

今回は残念ながらUnratedでした

A Circle Pond

円周の式に当てはめるだけ。円周率は3.14とかではなくmathのpi使いましょう。

ABC163a.py
import math

a=int(input())

print(math.pi*2*a)

B Homework

夏休みの日数-宿題の総日数の差を出力する。

ABC163b.py
n,m=map(int,input().split())
a=list(map(int,input().split()))
for i in a:
    n-=i
    if n<0: #宿題の総日数が夏休みの日数を上回った場合
        print(-1)
        exit()
print(n)

C Management

上司1,2...nに対応する配列を用意し、$A_n$が指し示す上司を$+1$する。
配列が0から始まるのでそこだけ気を付ける。

ABC163c.py
n=int(input())
l=list(map(int,input().split()))

ans=[0]*n

for i in l:
    ans[i-1]+=1

for j in ans:
    print(j)

D Sum of Large Numbers

Nが$10^{100}$より十分小さいため、$10^{100}+N$個の中から$x$個を選んだ時と$y$個の数を選んだ時の和が同じになることはない。このため$k$個を選んだ時の中で異なる組み合わせを考えればOK。また、組み合わせの和を計算するときは$10^{100}$の部分は無視してOK。
$N$個の0から始まる連続する整数のうち$i$個選んだ組み合わせの総数は、$\sum_{k=i}^{N}k$と$\sum_{k=0}^{i-1}k$の差になる。
Σ計算はfor文回すとTLE確実なので、中学校で習ったガウスの和の公式を使いましょう。

ABC163.py
n,k=map(int,input().split())
ans=0
for i in range(k,n+2):
    a=0.5*i*(i-1)
    b=0.5*n*(n+1)-0.5*(n-i)*(n-i+1)
    ans+=int(b-a+1)


print(ans%(10**9+7))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0