Closing Early

면접 대비

시간 제한1초메모리 제한2048 MB

요약
앞에서부터 k명에게 주문을 받았을 때 주문량 합이 R과 S로 나눈 나머지가 같아지는 최소 k를 구하고, 없으면 -1을 출력한다.
난이도

쉬움10점 중 3점

유형
누적 합, 해시맵, 수학, 구현
정답자
아직 제출이 없습니다

문제

Byron, the owner of a popular pizza shop renowned for its exclusive Hawaiian pizza, faces a unique challenge today. His shop operates on a strict policy: no pizza slices are wasted at the end of the day. Byron, an avid gamer, is eager to wrap up his day to play his favorite game, Hyper Space Pixel Conquerors (HSPC). However, he needs to ensure that he doesn't have any leftover pizza slices before he leaves.

At the start of the day, Byron's shop has exactly RR slices of Hawaiian pizza ready. Each whole pizza has exact SS slices of pizza. Throughout the day, NN customers will visit his shop in a sequential order. The ithi^{\text{th}} customer will order A_iA\_i slices of pizza. Byron, will prepare pizzas as necessary. That is, he will prepare an additional pizza only if he does not have enough slices of pizza ready to serve the current customer.

Byron's goal is to find the smallest number kk such that, after serving the first kk customers, he can close the shop without any pizza going to waste. The rest of the customers will be turned away. This occurs when the total number of pizza slices served, minus the initial RR slices, is exactly divisible by SS. Formally, he wants to find the smallest number kk such that: \[ A_1 + A_2 + \ldots + A_k \equiv R \pmod{S}. \] Note that kk may be 00 if R=0R = 0 and no customers should be served.

입력

The first line containing three space-separated integers R,S,NR,S,N (0≤R<S≤1090 \leq R < S \leq 10^9, 1≤N≤1051 \leq N \leq 10^5) --- the remaining slices at the start of the day, the number of slices in a single pizza, and the number of customers, respectively.

The second line contains NN integers, where the ithi^{\text{th}} integer is A_iA\_i (0<A_i≤1090 < A\_i \leq 10^9) --- the number of slices of pizza the ithi^{\text{th}} customer will order.

출력

A single integer kk, the smallest number such that, after serving kk customers, Byron will have no pizza left over. Note that it may be the case that k=Nk=N. If Byron cannot close the shop without leftovers, then output −1-1.

예제3

  1. 예제 1

    입력
    7 10 5
    2 1 4 12 6
    
    예상 출력
    3
    
  2. 예제 2

    입력
    0 7 3
    1 2 3
    
    예상 출력
    0
    
  3. 예제 3

    입력
    5 9 7
    4 2 1 3 3 6 3
    
    예상 출력
    -1