From 4fac6c8019a23037f52d640fcdd4aca0f9f77088 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 5 Oct 2025 23:06:38 -0400 Subject: [PATCH] feat(tests): fixtures --- scrapers/cses.py | 2 +- tests/conftest.py | 164 +- tests/fixtures/atcoder_task_abc100_a.html | 1483 +++++++----------- tests/fixtures/atcoder_task_abc100_b.html | 1483 +++++++----------- tests/fixtures/atcoder_task_abc100_c.html | 618 ++++++++ tests/fixtures/atcoder_task_abc100_d.html | 728 +++++++++ tests/fixtures/codeforces_1550_problems.html | 1095 +++++++++++++ 7 files changed, 3750 insertions(+), 1823 deletions(-) create mode 100644 tests/fixtures/atcoder_task_abc100_c.html create mode 100644 tests/fixtures/atcoder_task_abc100_d.html create mode 100644 tests/fixtures/codeforces_1550_problems.html diff --git a/scrapers/cses.py b/scrapers/cses.py index 5302caa..2f76cc5 100644 --- a/scrapers/cses.py +++ b/scrapers/cses.py @@ -19,7 +19,7 @@ from .models import ( ) BASE_URL = "https://cses.fi" -INDEX_PATH = "/problemset/list" +INDEX_PATH = "/problemset" TASK_PATH = "/problemset/task/{id}" HEADERS = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" diff --git a/tests/conftest.py b/tests/conftest.py index 0843a4d..a2efcc0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,16 @@ +import asyncio import importlib.util import io import json import sys from pathlib import Path -from types import ModuleType +from types import SimpleNamespace +from typing import Any +import httpx import pytest +import requests +from scrapling import fetchers ROOT = Path(__file__).resolve().parent.parent FIX = Path(__file__).resolve().parent / "fixtures" @@ -20,12 +25,12 @@ def fixture_text(): return _load -def _load_scraper_module(module_path: Path, module_name: str) -> ModuleType: +def _load_scraper_module(module_path: Path, module_name: str): spec = importlib.util.spec_from_file_location( f"scrapers.{module_name}", module_path ) if spec is None or spec.loader is None: - raise ImportError(f"Could not load spec for {module_name} from {module_path}") + raise ImportError(f"Cannot load module {module_name}") module = importlib.util.module_from_spec(spec) sys.modules[f"scrapers.{module_name}"] = module spec.loader.exec_module(module) @@ -33,8 +38,6 @@ def _load_scraper_module(module_path: Path, module_name: str) -> ModuleType: def _capture_stdout(coro): - import asyncio - buf = io.StringIO() old = sys.stdout sys.stdout = buf @@ -49,12 +52,26 @@ def _capture_stdout(coro): @pytest.fixture def run_scraper_offline(fixture_text): def _router_cses(*, path: str | None = None, url: str | None = None) -> str: - if path == "/problemset/list": + if not path and not url: + raise AssertionError("CSES expects path or url") + + target = path or url + if target is None: + raise AssertionError(f"No target for CSES (path={path!r}, url={url!r})") + + if target.startswith("https://cses.fi"): + target = target.removeprefix("https://cses.fi") + + if target.strip("/") == "problemset": return fixture_text("cses_contests.html") - if path and path.startswith("/problemset/task/"): - pid = path.rsplit("/", 1)[-1] + + if target.startswith("/problemset/task/") or target.startswith( + "problemset/task/" + ): + pid = target.rstrip("/").split("/")[-1] return fixture_text(f"cses_task_{pid}.html") - raise AssertionError(f"No fixture for CSES path={path!r}") + + raise AssertionError(f"No fixture for CSES path={path!r} url={url!r}") def _router_atcoder(*, path: str | None = None, url: str | None = None) -> str: if not url: @@ -71,6 +88,9 @@ def run_scraper_offline(fixture_text): def _router_codeforces(*, path: str | None = None, url: str | None = None) -> str: if not url: raise AssertionError("Codeforces expects url routing") + if "/contest/" in url and url.endswith("/problems"): + contest_id = url.rstrip("/").split("/")[-2] + return fixture_text(f"codeforces_{contest_id}_problems.html") if "/contests" in url and "/problem/" not in url: return fixture_text("codeforces_contests.html") if "/problem/" in url: @@ -81,58 +101,99 @@ def run_scraper_offline(fixture_text): parts = url.rstrip("/").split("/") contest_id, index = parts[-2], parts[-1] return fixture_text(f"codeforces_{contest_id}_{index}.html") + raise AssertionError(f"No fixture for Codeforces url={url!r}") def _make_offline_fetches(scraper_name: str): - if scraper_name == "cses": + match scraper_name: + case "cses": - def __offline_fetch_text(client, path: str) -> str: - return _router_cses(path=path) + async def __offline_fetch_text(client, path: str, **kwargs): + html = _router_cses(path=path) + return SimpleNamespace( + text=html, + status_code=200, + raise_for_status=lambda: None, + ) - return { - "__offline_fetch_text": __offline_fetch_text, - "__offline_fetch_sync": lambda url: (_ for _ in ()).throw( - AssertionError("CSES doesn't use _fetch") - ), - "__offline_fetch_async": lambda client, url: (_ for _ in ()).throw( - AssertionError("CSES doesn't use _get_async") - ), - } - if scraper_name == "atcoder": + return { + "__offline_fetch_text": __offline_fetch_text, + } - async def __offline_fetch_async(client, url: str) -> str: - return _router_atcoder(url=url) + case "atcoder": - def __offline_fetch_sync(url: str) -> str: - return _router_atcoder(url=url) + def __offline_fetch(url: str, *args, **kwargs): + html = _router_atcoder(url=url) + return html - return { - "__offline_fetch_text": lambda client, path: (_ for _ in ()).throw( - AssertionError("AtCoder doesn't use fetch_text") - ), - "__offline_fetch_sync": __offline_fetch_sync, - "__offline_fetch_async": __offline_fetch_async, - } - if scraper_name == "codeforces": + async def __offline_get_async(client, url: str, **kwargs): + return _router_atcoder(url=url) - def __offline_fetch_sync(url: str) -> str: - return _router_codeforces(url=url) + return { + "_fetch": __offline_fetch, + "_get_async": __offline_get_async, + } - return { - "__offline_fetch_text": lambda client, path: (_ for _ in ()).throw( - AssertionError("Codeforces doesn't use fetch_text") - ), - "__offline_fetch_sync": __offline_fetch_sync, - "__offline_fetch_async": lambda client, url: (_ for _ in ()).throw( - AssertionError("Codeforces doesn't use _get_async") - ), - } - raise AssertionError(f"Unknown scraper: {scraper_name}") + case "codeforces": + + class MockPage: + def __init__(self, html: str): + self.html_content = html + + def _mock_stealthy_fetch(url: str, **kwargs): + return MockPage(_router_codeforces(url=url)) + + def _mock_requests_get(url: str, **kwargs): + if "api/contest.list" in url: + data = { + "status": "OK", + "result": [ + { + "id": 1550, + "name": "Educational Codeforces Round 155 (Rated for Div. 2)", + "phase": "FINISHED", + }, + { + "id": 1000, + "name": "Codeforces Round #1000", + "phase": "FINISHED", + }, + ], + } + + class R: + def json(self_inner): + return data + + def raise_for_status(self_inner): + return None + + return R() + raise AssertionError(f"Unexpected requests.get call: {url}") + + return { + "StealthyFetcher.fetch": _mock_stealthy_fetch, + "requests.get": _mock_requests_get, + } + + case _: + raise AssertionError(f"Unknown scraper: {scraper_name}") def _run(scraper_name: str, mode: str, *args: str): mod_path = ROOT / "scrapers" / f"{scraper_name}.py" ns = _load_scraper_module(mod_path, scraper_name) - main_async = getattr(ns, "main_async", None) + offline_fetches = _make_offline_fetches(scraper_name) + + if scraper_name == "codeforces": + fetchers.stealthyfetcher.fetch = offline_fetches["stealthyfetcher.fetch"] # type: ignore + requests.get = offline_fetches["requests.get"] + elif scraper_name == "atcoder": + ns._fetch = offline_fetches["_fetch"] + ns._get_async = offline_fetches["_get_async"] + elif scraper_name == "cses": + httpx.asyncclient.get = offline_fetches["__offline_fetch_text"] # type: ignore + + main_async = getattr(ns, "main_async") assert callable(main_async), f"main_async not found in {scraper_name}" argv = [str(mod_path), mode, *args] @@ -143,14 +204,9 @@ def run_scraper_offline(fixture_text): finally: sys.argv = old_argv - json_lines = [] + json_lines: list[Any] = [] for line in (l for l in out.splitlines() if l.strip()): - try: - json_lines.append(json.loads(line)) - except json.JSONDecodeError as e: - raise AssertionError( - f"Invalid JSON from {scraper_name} {mode}: {line}" - ) from e + json_lines.append(json.loads(line)) return rc, json_lines return _run diff --git a/tests/fixtures/atcoder_task_abc100_a.html b/tests/fixtures/atcoder_task_abc100_a.html index c96cd9a..2e0e623 100644 --- a/tests/fixtures/atcoder_task_abc100_a.html +++ b/tests/fixtures/atcoder_task_abc100_a.html @@ -1,885 +1,602 @@ - + + + + + + + + + - - A - Happy Birthday! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - -
- -
-
-
-
-
- - Contest Duration: - - - - - (local time) (100 minutes) - - Back to Home -
- -
-
- - A - Happy Birthday! - Editorial - - - / - - -
-

Time Limit: 2 sec / Memory Limit: 976 MiB

- -
- - -

配点: 100

- -
-
-

問題文

-

- もうすぐ E869120 君と square1001 君の - 16 才の誕生日が来る.
- そこで, AtCoder 王国の高橋君は, 円形のケーキ - 1 個に放射状に切れ目を入れ - 16 等分したものを, 彼らにプレゼントした. -

-

- E869120 君はそのうち A 切れ、square1001 君は - B 切れを食べようとした.
- しかし, ケーキと一緒についていた紙を見ると, - 「同じ人が隣り合う - 2 - 切れのケーキを両方取ってはならない」と書かれていた. -

-

- さて、彼らは紙に書かれたことを守って、2 - 人とも食べたい数のケーキを取ることができるだろうか? -

-
-
- -
-
-

制約

-
    -
  • - A, B1 以上 - 16 以下の整数 -
  • -
  • A+B16 以下である.
  • -
-
-
- -
- -
-
-
-

入力

-

入力は以下の形式で標準入力から与えられる.

-
A B
-
-
-
- -
-
-

出力

-

- 紙に書かれたことを守って, E869120 君と square1001 - 君両方が, 食べたい数のケーキを取ることができるならば - Yay!, そうでなければ - :( と出力しなさい. -

-
-
-
- -
- -
-
-

入力例 1

-
-5 4
-
-
-
- -
-
-

出力例 1

-
-Yay!
-
- -

- 下の図のようにケーキを取れば、2 - 人とも目標を達成することができる.
-  -

-
-
- -
- -
-
-

入力例 2

-
-8 8
-
-
-
- -
-
-

出力例 2

-
-Yay!
-
- -

- 下の図のようにケーキを取れば、2 - 人とも目標を達成することができる.
-  -

-
-
- -
- -
-
-

入力例 3

-
-11 4
-
-
-
- -
-
-

出力例 3

-
-:(
-
- -

- この場合, 残念ながら目標を達成する方法は - 1 つもない. -

-
-
-
- -

Score: 100 points

- -
-
-

Problem Statement

-

- E869120's and square1001's 16-th birthday is - coming soon.
- Takahashi from AtCoder Kingdom gave them a round cake - cut into 16 equal fan-shaped pieces. -

-

- E869120 and square1001 were just about to eat - A and B of those pieces, - respectively,
- when they found a note attached to the cake saying that - "the same person should not take two adjacent pieces of - cake". -

-

- Can both of them obey the instruction in the note and - take desired numbers of pieces of cake? -

-
-
- -
-
-

Constraints

-
    -
  • - A and B are integers between - 1 and 16 (inclusive). -
  • -
  • A+B is at most 16.
  • -
-
-
- -
- -
-
-
-

Input

-

- Input is given from Standard Input in the following - format: -

-
A B
-
-
-
- -
-
-

Output

-

- If both E869120 and square1001 can obey the - instruction in the note and take desired numbers of - pieces of cake, print Yay!; otherwise, - print :(. -

-
-
-
- -
- -
-
-

Sample Input 1

-
-5 4
-
-
-
- -
-
-

Sample Output 1

-
-Yay!
-
- -

- Both of them can take desired number of pieces as - follows: -  -

-
-
- -
- -
-
-

Sample Input 2

-
-8 8
-
-
-
- -
-
-

Sample Output 2

-
-Yay!
-
- -

- Both of them can take desired number of pieces as - follows: -  -

-
-
- -
- -
-
-

Sample Input 3

-
-11 4
-
-
-
- -
-
-

Sample Output 3

-
-:(
-
- -

- In this case, there is no way for them to take desired - number of pieces, unfortunately. -

-
-
-
-
-
-
-
- -
- -
- - - - - - -
- - -
-
-
- -
- -
-

- - + + A - Happy Birthday! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ +
+
+ + + +
+
+
+ + + Contest Duration: + - (local time) + (100 minutes) + + + Back to Home +
+ +
+
+ + A - Happy Birthday! + Editorial + + / + +
+

+ Time Limit: 2 sec / Memory Limit: 976 MiB + + +

+ +
+ + + + + + + + +

配点: 100

+ +
+
+

問題文

+

もうすぐ E869120 君と square1001 君の 16 才の誕生日が来る.
+そこで, AtCoder 王国の高橋君は, 円形のケーキ 1 個に放射状に切れ目を入れ 16 等分したものを, 彼らにプレゼントした.

+

E869120 君はそのうち A 切れ、square1001 君は B 切れを食べようとした.
+しかし, ケーキと一緒についていた紙を見ると, 「同じ人が隣り合う 2 切れのケーキを両方取ってはならない」と書かれていた.

+

さて、彼らは紙に書かれたことを守って、2 人とも食べたい数のケーキを取ることができるだろうか?

+
+
+ +
+
+

制約

+
    +
  • A, B1 以上 16 以下の整数
  • +
  • A+B16 以下である.
  • +
+
+
+ +
+ +
+
+
+

入力

+

入力は以下の形式で標準入力から与えられる.

+
A B
+
+ +
+
+ +
+
+

出力

+

紙に書かれたことを守って, E869120 君と square1001 君両方が, 食べたい数のケーキを取ることができるならば Yay!, そうでなければ :( と出力しなさい.

+
+
+
+ +
+ +
+
+

入力例 1

5 4
+
+ +
+
+ +
+
+

出力例 1

Yay!
+
+ +

下の図のようにケーキを取れば、2 人とも目標を達成することができる.
+

+
+
+ +
+ +
+
+

入力例 2

8 8
+
+ +
+
+ +
+
+

出力例 2

Yay!
+
+ +

下の図のようにケーキを取れば、2 人とも目標を達成することができる.
+

+
+
+ +
+ +
+
+

入力例 3

11 4
+
+ +
+
+ +
+
+

出力例 3

:(
+
+ +

この場合, 残念ながら目標を達成する方法は 1 つもない.

+
+
+ +

Score: 100 points

+ +
+
+

Problem Statement

+

E869120's and square1001's 16-th birthday is coming soon.
+Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

+

E869120 and square1001 were just about to eat A and B of those pieces, respectively,
+when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".

+

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

+
+
+ +
+
+

Constraints

+
    +
  • A and B are integers between 1 and 16 (inclusive).
  • +
  • A+B is at most 16.
  • +
+
+
+ +
+ +
+
+
+

Input

+

Input is given from Standard Input in the following format:

+
A B
+
+ +
+
+ +
+
+

Output

+

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

+
+
+
+ +
+ +
+
+

Sample Input 1

5 4
+
+ +
+
+ +
+
+

Sample Output 1

Yay!
+
+ +

Both of them can take desired number of pieces as follows: +

+
+
+ +
+ +
+
+

Sample Input 2

8 8
+
+ +
+
+ +
+
+

Sample Output 2

Yay!
+
+ +

Both of them can take desired number of pieces as follows: +

+
+
+ +
+ +
+
+

Sample Input 3

11 4
+
+ +
+
+ +
+
+

Sample Output 3

:(
+
+ +

In this case, there is no way for them to take desired number of pieces, unfortunately.

+
+
+
+ +
+ + + + +
+
+ + + + + +
+ + + +
+ + + + + + +
+ + + + +
+
+
+ +
+ +
+

+ + + + + diff --git a/tests/fixtures/atcoder_task_abc100_b.html b/tests/fixtures/atcoder_task_abc100_b.html index c2ab95e..c7a28d3 100644 --- a/tests/fixtures/atcoder_task_abc100_b.html +++ b/tests/fixtures/atcoder_task_abc100_b.html @@ -1,887 +1,600 @@ - + + + + + + + + + - - B - Ringo's Favorite Numbers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - -
- -
-
-
-
-
- - Contest Duration: - - - - - (local time) (100 minutes) - - Back to Home -
- -
-
- - B - Ringo's Favorite Numbers - Editorial - - - / - - -
-

Time Limit: 2 sec / Memory Limit: 976 MiB

- -
- - -

配点: 200

- -
-
-

問題文

-

- 今日は, 記念すべき AtCoder Beginner Contest 100 - が開催される. そのため, 高橋君はりんごさんに, - ある整数をプレゼントしようと思った.
- 今日のコンテストは「AtCoder Beginner Contest - 100」なので, りんごさんは 100 で - ちょうど - D - 回割りきれる正の整数をプレゼントされると喜ぶ. -

-

- さて, りんごさんがプレゼントされると喜ぶような整数のうち - N 番目に小さいものを求めなさい. -

-
-
- -
-
-

制約

-
    -
  • - D0, 1, 2 のいずれかである -
  • -
  • - N1 以上 - 100 以下の整数 -
  • -
-
-
- -
- -
-
-
-

入力

-

入力は以下の形式で標準入力から与えられる.

-
D N
-
-
-
- -
-
-

出力

-

- 100 でちょうど - D 回割りきれる正の整数の中で - N 番目に小さいものを出力しなさい. -

-
-
-
- -
- -
-
-

入力例 1

-
-0 5
-
-
-
- -
-
-

出力例 1

-
-5
-
- -

- 100 でちょうど - 0 回割り切れる(すなわち, - 100 で割り切れない)整数は, 1, - 2, 3, 4, 5, - 6, 7, ... と続く.
- よって, 5 番目に小さいりんごさんが喜ぶ整数は - 5 である. -

-
-
- -
- -
-
-

入力例 2

-
-1 11
-
-
-
- -
-
-

出力例 2

-
-1100
-
- -

- 100 でちょうど - 1 回割り切れる整数は, 100, - 200, 300, 400, - 500, 600, 700, - 800, 900, 1 \ 000, - 1 \ 100, ... と続く.
- よって, 求めたい整数は 1 \ 100 である. -

-
-
- -
- -
-
-

入力例 3

-
-2 85
-
-
-
- -
-
-

出力例 3

-
-850000
-
- -

- 100 でちょうど - 2 回割り切れる整数は, 10 \ 000, - 20 \ 000, 30 \ 000, ... と続く.
- よって, 求めたい整数は 850 \ 000 である. -

-
-
-
- -

Score: 200 points

- -
-
-

Problem Statement

-

- Today, the memorable AtCoder Beginner Contest 100 takes - place. On this occasion, Takahashi would like to give an - integer to Ringo.
- As the name of the contest is AtCoder Beginner Contest - 100, Ringo would be happy if he is given a positive - integer that can be divided by 100 - exactly D times. -

-

- Find the N-th smallest integer that would - make Ringo happy. -

-
-
- -
-
-

Constraints

-
    -
  • - D is 0, 1 or - 2. -
  • -
  • - N is an integer between 1 and - 100 (inclusive). -
  • -
-
-
- -
- -
-
-
-

Input

-

- Input is given from Standard Input in the following - format: -

-
D N
-
-
-
- -
-
-

Output

-

- Print the N-th smallest integer that can be - divided by 100 exactly D times. -

-
-
-
- -
- -
-
-

Sample Input 1

-
-0 5
-
-
-
- -
-
-

Sample Output 1

-
-5
-
- -

- The integers that can be divided by - 100 exactly 0 times (that is, not - divisible by 100) are as follows: - 1, 2, 3, 4, - 5, 6, 7, ...
- Thus, the 5-th smallest integer that would - make Ringo happy is 5. -

-
-
- -
- -
-
-

Sample Input 2

-
-1 11
-
-
-
- -
-
-

Sample Output 2

-
-1100
-
- -

- The integers that can be divided by - 100 exactly once are as follows: - 100, 200, 300, - 400, 500, 600, - 700, 800, 900, - 1 \ 000, 1 \ 100, ...
- Thus, the integer we are seeking is 1 \ 100. -

-
-
- -
- -
-
-

Sample Input 3

-
-2 85
-
-
-
- -
-
-

Sample Output 3

-
-850000
-
- -

- The integers that can be divided by - 100 exactly twice are as follows: - 10 \ 000, 20 \ 000, - 30 \ 000, ...
- Thus, the integer we are seeking is - 850 \ 000. -

-
-
-
-
-
-
-
- -
- -
- - - - - - -
- - -
-
-
- -
- -
-

- - + + B - Ringo's Favorite Numbers + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ +
+
+ + + +
+
+
+ + + Contest Duration: + - (local time) + (100 minutes) + + + Back to Home +
+ +
+
+ + B - Ringo's Favorite Numbers + Editorial + + / + +
+

+ Time Limit: 2 sec / Memory Limit: 976 MiB + + +

+ +
+ + + + + + + + +

配点: 200

+ +
+
+

問題文

+

今日は, 記念すべき AtCoder Beginner Contest 100 が開催される. そのため, 高橋君はりんごさんに, ある整数をプレゼントしようと思った.
+今日のコンテストは「AtCoder Beginner Contest 100」なので, りんごさんは 100ちょうど D 回割りきれる正の整数をプレゼントされると喜ぶ.

+

さて, りんごさんがプレゼントされると喜ぶような整数のうち N 番目に小さいものを求めなさい.

+
+
+ +
+
+

制約

+
    +
  • D0, 1, 2 のいずれかである
  • +
  • N1 以上 100 以下の整数
  • +
+
+
+ +
+ +
+
+
+

入力

+

入力は以下の形式で標準入力から与えられる.

+
D N
+
+ +
+
+ +
+
+

出力

+

100 でちょうど D 回割りきれる正の整数の中で N 番目に小さいものを出力しなさい.

+
+
+
+ +
+ +
+
+

入力例 1

0 5
+
+ +
+
+ +
+
+

出力例 1

5
+
+ +

100 でちょうど 0 回割り切れる(すなわち, 100 で割り切れない)整数は, 1, 2, 3, 4, 5, 6, 7, ... と続く.
+よって, 5 番目に小さいりんごさんが喜ぶ整数は 5 である.

+
+
+ +
+ +
+
+

入力例 2

1 11
+
+ +
+
+ +
+
+

出力例 2

1100
+
+ +

100 でちょうど 1 回割り切れる整数は, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1 \ 000, 1 \ 100, ... と続く.
+よって, 求めたい整数は 1 \ 100 である.

+
+
+ +
+ +
+
+

入力例 3

2 85
+
+ +
+
+ +
+
+

出力例 3

850000
+
+ +

100 でちょうど 2 回割り切れる整数は, 10 \ 000, 20 \ 000, 30 \ 000, ... と続く.
+よって, 求めたい整数は 850 \ 000 である.

+
+
+ +

Score: 200 points

+ +
+
+

Problem Statement

+

Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.
+As the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times.

+

Find the N-th smallest integer that would make Ringo happy.

+
+
+ +
+
+

Constraints

+
    +
  • D is 0, 1 or 2.
  • +
  • N is an integer between 1 and 100 (inclusive).
  • +
+
+
+ +
+ +
+
+
+

Input

+

Input is given from Standard Input in the following format:

+
D N
+
+ +
+
+ +
+
+

Output

+

Print the N-th smallest integer that can be divided by 100 exactly D times.

+
+
+
+ +
+ +
+
+

Sample Input 1

0 5
+
+ +
+
+ +
+
+

Sample Output 1

5
+
+ +

The integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ...
+Thus, the 5-th smallest integer that would make Ringo happy is 5.

+
+
+ +
+ +
+
+

Sample Input 2

1 11
+
+ +
+
+ +
+
+

Sample Output 2

1100
+
+ +

The integers that can be divided by 100 exactly once are as follows: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1 \ 000, 1 \ 100, ...
+Thus, the integer we are seeking is 1 \ 100.

+
+
+ +
+ +
+
+

Sample Input 3

2 85
+
+ +
+
+ +
+
+

Sample Output 3

850000
+
+ +

The integers that can be divided by 100 exactly twice are as follows: 10 \ 000, 20 \ 000, 30 \ 000, ...
+Thus, the integer we are seeking is 850 \ 000.

+
+
+
+ +
+ + + + +
+
+ + + + + +
+ + + +
+ + + + + + +
+ + + + +
+
+
+ +
+ +
+

+ + + + + diff --git a/tests/fixtures/atcoder_task_abc100_c.html b/tests/fixtures/atcoder_task_abc100_c.html new file mode 100644 index 0000000..6b298ba --- /dev/null +++ b/tests/fixtures/atcoder_task_abc100_c.html @@ -0,0 +1,618 @@ + + + + + + + + + + + + C - *3 or /2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ +
+
+ + + +
+
+
+ + + Contest Duration: + - (local time) + (100 minutes) + + + Back to Home +
+ +
+
+ + C - *3 or /2 + Editorial + + / + +
+

+ Time Limit: 2 sec / Memory Limit: 976 MiB + + +

+ +
+ + + + + + + + +

配点: 300

+ +
+
+

問題文

+

AtCoder Beginner Contest 100 の開催にともなって, AtCoder 社では長さ N の数列 a = {a_1, a_2, a_3, ..., a_N} が飾られることになった.
+社員のすぬけ君は, この数列で遊んでみようと思った.

+

具体的には, 以下の操作をできるだけ多くの回数繰り返そうと思った.

+
1 \leq i \leq N を満たす全ての i に対して, それぞれ「a_i の値を 2 で割る」「a_i の値を 3 倍する」のどちらかを行う.  
+ただし, 全ての i に対して 3 倍することはできず, 操作後の a_i の値は整数でなければならない.  
+
+ +

最大で何回の操作が可能か, 求めなさい.

+
+
+ +
+
+

制約

+
    +
  • N1 以上 10 \ 000 以下の整数
  • +
  • a_i1 以上 1 \ 000 \ 000 \ 000 以下の整数
  • +
+
+
+ +
+ +
+
+
+

入力

+

入力は以下の形式で標準入力から与えられる.

+
N
+a_1 a_2 a_3 ... a_N
+
+ +
+
+ +
+
+

出力

+

すぬけ君が行える最大の操作回数を出力しなさい.

+
+
+
+ +
+ +
+
+

入力例 1

3
+5 2 4
+
+ +
+
+ +
+
+

出力例 1

3
+
+ +

最初, 数列は {5, 2, 4} であるが, 以下のように操作すれば 3 回の操作を行うことができる.

+
    +
  • 最初に, a_13 倍し, a_23 倍し, a_32 で割る. すると数列は {15, 6, 2} となる.
  • +
  • 次に, a_13 倍し, a_22 で割り, a_33 倍する. すると数列は {45, 3, 6} となる.
  • +
  • 最後に, a_13 倍し, a_23 倍し, a_32 で割る. すると数列は {135, 9, 3} となる.
  • +
+
+
+ +
+ +
+
+

入力例 2

4
+631 577 243 199
+
+ +
+
+ +
+
+

出力例 2

0
+
+ +

全ての要素が奇数なので, 操作はできない. よって答えは 0 である.

+
+
+ +
+ +
+
+

入力例 3

10
+2184 2126 1721 1800 1024 2528 3360 1945 1280 1776
+
+ +
+
+ +
+
+

出力例 3

39
+
+
+
+ +

Score: 300 points

+ +
+
+

Problem Statement

+

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.
+Snuke, an employee, would like to play with this sequence.

+

Specifically, he would like to repeat the following operation as many times as possible:

+
For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3".  
+Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer.
+
+ +

At most how many operations can be performed?

+
+
+ +
+
+

Constraints

+
    +
  • N is an integer between 1 and 10 \ 000 (inclusive).
  • +
  • a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).
  • +
+
+
+ +
+ +
+
+
+

Input

+

Input is given from Standard Input in the following format:

+
N
+a_1 a_2 a_3 ... a_N
+
+ +
+
+ +
+
+

Output

+

Print the maximum number of operations that Snuke can perform.

+
+
+
+ +
+ +
+
+

Sample Input 1

3
+5 2 4
+
+ +
+
+ +
+
+

Sample Output 1

3
+
+ +

The sequence is initially {5, 2, 4}. Three operations can be performed as follows:

+
    +
  • First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.
  • +
  • Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.
  • +
  • Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.
  • +
+
+
+ +
+ +
+
+

Sample Input 2

4
+631 577 243 199
+
+ +
+
+ +
+
+

Sample Output 2

0
+
+ +

No operation can be performed since all the elements are odd. Thus, the answer is 0.

+
+
+ +
+ +
+
+

Sample Input 3

10
+2184 2126 1721 1800 1024 2528 3360 1945 1280 1776
+
+ +
+
+ +
+
+

Sample Output 3

39
+
+
+
+
+ +
+ + + + +
+
+ + + + + +
+ + + +
+ + + + + + +
+ + + + +
+
+
+ +
+ +
+

+ + + + + + diff --git a/tests/fixtures/atcoder_task_abc100_d.html b/tests/fixtures/atcoder_task_abc100_d.html new file mode 100644 index 0000000..552b276 --- /dev/null +++ b/tests/fixtures/atcoder_task_abc100_d.html @@ -0,0 +1,728 @@ + + + + + + + + + + + + D - Patisserie ABC + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ +
+
+ + + +
+
+
+ + + Contest Duration: + - (local time) + (100 minutes) + + + Back to Home +
+ +
+
+ + D - Patisserie ABC + Editorial + + / + +
+

+ Time Limit: 2 sec / Memory Limit: 976 MiB + + +

+ +
+ + + + + + + + +

配点: 400

+ +
+
+

問題文

+

高橋君はプロのパティシエになり, AtCoder Beginner Contest 100 を記念して, 「ABC洋菓子店」というお店を開いた.

+

ABC洋菓子店では, N 種類のケーキを売っている.
+各種類のケーキには「綺麗さ」「おいしさ」「人気度」の 3 つの値を持ち, i 種類目のケーキの綺麗さは x_i, おいしさは y_i, 人気度は z_i である.
+これらの値は 0 以下である可能性もある.

+

りんごさんは, ABC洋菓子店で M 個のケーキを食べることにした. 彼は次のように, 食べるケーキの組み合わせを選ぶことにした.

+
    +
  • 同じ種類のケーキを 2 個以上食べない.
  • +
  • 上の条件を満たしつつ, (綺麗さの合計の絶対値) + (おいしさの合計の絶対値) + (人気度の合計の絶対値) が最大になるように選ぶ.
  • +
+

このとき, りんごさんが選ぶケーキの (綺麗さの合計の絶対値) + (おいしさの合計の絶対値) + (人気度の合計の絶対値) の最大値を求めなさい.

+
+
+ +
+
+

制約

+
    +
  • N1 以上 1 \ 000 以下の整数
  • +
  • M0 以上 N 以下の整数
  • +
  • x_i, y_i, z_i \ (1 \leq i \leq N) は, それぞれ -10 \ 000 \ 000 \ 000 以上 10 \ 000 \ 000 \ 000 以下の整数.
  • +
+
+
+ +
+ +
+
+
+

入力

+

入力は以下の形式で標準入力から与えられる.

+
N M
+x_1 y_1 z_1
+x_2 y_2 z_2
+ :  :
+x_N y_N z_N
+
+ +
+
+ +
+
+

出力

+

りんごさんが選ぶケーキの (綺麗さの合計の絶対値) + (おいしさの合計の絶対値) + (人気度の合計の絶対値) の最大値を出力しなさい.

+
+
+
+ +
+ +
+
+

入力例 1

5 3
+3 1 4
+1 5 9
+2 6 5
+3 5 8
+9 7 9
+
+ +
+
+ +
+
+

出力例 1

56
+
+ +

2, 4, 5 種類目のケーキを食べることを考える. そのとき, 「綺麗さ」「おいしさ」「人気度」の合計はそれぞれ次のようになる.

+
    +
  • 綺麗さ:1 + 3 + 9 = 13
  • +
  • おいしさ:5 + 5 + 7 = 17
  • +
  • 人気度:9 + 8 + 9 = 26
  • +
+

このときの (綺麗さの合計の絶対値) + (おいしさの合計の絶対値) + (人気度の合計の絶対値) は 13 + 17 + 26 = 56 となり, これが最大になる.

+
+
+ +
+ +
+
+

入力例 2

5 3
+1 -2 3
+-4 5 -6
+7 -8 -9
+-10 11 -12
+13 -14 15
+
+ +
+
+ +
+
+

出力例 2

54
+
+ +

1, 3, 5 種類目のケーキを食べることを考える. そのとき, 「綺麗さ」「おいしさ」「人気度」の合計はそれぞれ次のようになる.

+
    +
  • 綺麗さ:1 + 7 + 13 = 21
  • +
  • おいしさ:(-2) + (-8) + (-14) = -24
  • +
  • 人気度:3 + (-9) + 15 = 9
  • +
+

このときの (綺麗さの合計の絶対値) + (おいしさの合計の絶対値) + (人気度の合計の絶対値) は 21 + 24 + 9 = 54 となり, これが最大になる.

+
+
+ +
+ +
+
+

入力例 3

10 5
+10 -80 21
+23 8 38
+-94 28 11
+-26 -2 18
+-69 72 79
+-26 -86 -54
+-72 -50 59
+21 65 -32
+40 -94 87
+-62 18 82
+
+ +
+
+ +
+
+

出力例 3

638
+
+ +

3, 4, 5, 7, 10 種類目のケーキを食べると, 綺麗さの合計は -323, おいしさの合計は 66, 人気度の合計は 249 となる.
+このときの (綺麗さの合計の絶対値) + (おいしさの合計の絶対値) + (人気度の合計の絶対値) は 323 + 66 + 249 = 638 となり, これが最大になる.

+
+
+ +
+ +
+
+

入力例 4

3 2
+2000000000 -9000000000 4000000000
+7000000000 -5000000000 3000000000
+6000000000 -1000000000 8000000000
+
+ +
+
+ +
+
+

出力例 4

30000000000
+
+ +

ケーキの綺麗さ, おいしさ, 人気度や出力すべき値が, 32bit 整数に収まらない場合もある.

+
+
+ +

Score: 400 points

+ +
+
+

Problem Statement

+

Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100.

+

The shop sells N kinds of cakes.
+Each kind of cake has three parameters "beauty", "tastiness" and "popularity". The i-th kind of cake has the beauty of x_i, the tastiness of y_i and the popularity of z_i.
+These values may be zero or negative.

+

Ringo has decided to have M pieces of cakes here. He will choose the set of cakes as follows:

+
    +
  • Do not have two or more pieces of the same kind of cake.
  • +
  • Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).
  • +
+

Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

+
+
+ +
+
+

Constraints

+
    +
  • N is an integer between 1 and 1 \ 000 (inclusive).
  • +
  • M is an integer between 0 and N (inclusive).
  • +
  • x_i, y_i, z_i \ (1 \leq i \leq N) are integers between -10 \ 000 \ 000 \ 000 and 10 \ 000 \ 000 \ 000 (inclusive).
  • +
+
+
+ +
+ +
+
+
+

Input

+

Input is given from Standard Input in the following format:

+
N M
+x_1 y_1 z_1
+x_2 y_2 z_2
+ :  :
+x_N y_N z_N
+
+ +
+
+ +
+
+

Output

+

Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

+
+
+
+ +
+ +
+
+

Sample Input 1

5 3
+3 1 4
+1 5 9
+2 6 5
+3 5 8
+9 7 9
+
+ +
+
+ +
+
+

Sample Output 1

56
+
+ +

Consider having the 2-nd, 4-th and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

+
    +
  • Beauty: 1 + 3 + 9 = 13
  • +
  • Tastiness: 5 + 5 + 7 = 17
  • +
  • Popularity: 9 + 8 + 9 = 26
  • +
+

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56. This is the maximum value.

+
+
+ +
+ +
+
+

Sample Input 2

5 3
+1 -2 3
+-4 5 -6
+7 -8 -9
+-10 11 -12
+13 -14 15
+
+ +
+
+ +
+
+

Sample Output 2

54
+
+ +

Consider having the 1-st, 3-rd and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

+
    +
  • Beauty: 1 + 7 + 13 = 21
  • +
  • Tastiness: (-2) + (-8) + (-14) = -24
  • +
  • Popularity: 3 + (-9) + 15 = 9
  • +
+

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 21 + 24 + 9 = 54. This is the maximum value.

+
+
+ +
+ +
+
+

Sample Input 3

10 5
+10 -80 21
+23 8 38
+-94 28 11
+-26 -2 18
+-69 72 79
+-26 -86 -54
+-72 -50 59
+21 65 -32
+40 -94 87
+-62 18 82
+
+ +
+
+ +
+
+

Sample Output 3

638
+
+ +

If we have the 3-rd, 4-th, 5-th, 7-th and 10-th kinds of cakes, the total beauty, tastiness and popularity will be -323, 66 and 249, respectively.
+The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 323 + 66 + 249 = 638. This is the maximum value.

+
+
+ +
+ +
+
+

Sample Input 4

3 2
+2000000000 -9000000000 4000000000
+7000000000 -5000000000 3000000000
+6000000000 -1000000000 8000000000
+
+ +
+
+ +
+
+

Sample Output 4

30000000000
+
+ +

The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.

+
+
+
+ +
+ + + + +
+
+ + + + + +
+ + + +
+ + + + + + +
+ + + + +
+
+
+ +
+ +
+

+ + + + + + diff --git a/tests/fixtures/codeforces_1550_problems.html b/tests/fixtures/codeforces_1550_problems.html new file mode 100644 index 0000000..fa19bbb --- /dev/null +++ b/tests/fixtures/codeforces_1550_problems.html @@ -0,0 +1,1095 @@ +Problems - Codeforces
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
+
+ + +
Educational Codeforces Round 111 (Rated for Div. 2)
+
+ +
+
+ +
+
+ +
+ +
A. Find The Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's call an array a consisting of n positive (greater than 0) integers beautiful if the following condition is held for every i from 1 to n: either ai=1, or at least one of the numbers ai1 and ai2 exists in the array as well.

For example:

  • the array [5,3,1] is beautiful: for a1, the number a12=3 exists in the array; for a2, the number a22=1 exists in the array; for a3, the condition a3=1 holds;
  • the array [1,2,2,2,2] is beautiful: for a1, the condition a1=1 holds; for every other number ai, the number ai1=1 exists in the array;
  • the array [1,4] is not beautiful: for a2, neither a22=2 nor a21=3 exists in the array, and a21;
  • the array [2] is not beautiful: for a1, neither a11=1 nor a12=0 exists in the array, and a11;
  • the array [2,1,3] is beautiful: for a1, the number a11=1 exists in the array; for a2, the condition a2=1 holds; for a3, the number a32=1 exists in the array.

You are given a positive integer s. Find the minimum possible size of a beautiful array with the sum of elements equal to s.

Input

The first line contains one integer t (1t5000) — the number of test cases.

Then t lines follow, the i-th line contains one integer s (1s5000) for the i-th test case.

Output

Print t integers, the i-th integer should be the answer for the i-th testcase: the minimum possible size of a beautiful array with the sum of elements equal to s.

Example
Input
Copy
4
+1
+8
+7
+42
+
Output
Copy
1
+3
+3
+7
+
Note

Consider the example test:

  1. in the first test case, the array [1] meets all conditions;
  2. in the second test case, the array [3,4,1] meets all conditions;
  3. in the third test case, the array [1,2,4] meets all conditions;
  4. in the fourth test case, the array [1,4,6,8,10,2,11] meets all conditions.

+
+ +
+
+ + +
+ +
B. Maximum Cost Deletion
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s of length n consisting only of the characters 0 and 1.

You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length l, you get al+b points.

Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.

Input

The first line contains a single integer t (1t2000) — the number of testcases.

The first line of each testcase contains three integers n, a and b (1n100;100a,b100) — the length of the string s and the parameters a and b.

The second line contains the string s. The string s consists only of the characters 0 and 1.

Output

For each testcase, print a single integer — the maximum number of points that you can score.

Example
Input
Copy
3
+3 2 0
+000
+5 -2 5
+11001
+6 1 -4
+100111
+
Output
Copy
6
+15
+-2
+
Note

In the first example, it is enough to delete the entire string, then we will get 23+0=6 points.

In the second example, if we delete characters one by one, then for each deleted character we will get (2)1+5=3 points, i. e. 15 points in total.

In the third example, we can delete the substring 00 from the string 100111, we get 12+(4)=2 points, and the string will be equal to 1111, removing it entirely we get 14+(4)=0 points. In total, we got 2 points for 2 operations.

+
+ +
+
+ + +
+ +
C. Manhattan Subarrays
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Suppose you have two points p=(xp,yp) and q=(xq,yq). Let's denote the Manhattan distance between them as d(p,q)=|xpxq|+|ypyq|.

Let's say that three points p, q, r form a bad triple if d(p,r)=d(p,q)+d(q,r).

Let's say that an array b1,b2,,bm is good if it is impossible to choose three distinct indices i, j, k such that the points (bi,i), (bj,j) and (bk,k) form a bad triple.

You are given an array a1,a2,,an. Calculate the number of good subarrays of a. A subarray of the array a is the array al,al+1,,ar for some 1lrn.

Note that, according to the definition, subarrays of length 1 and 2 are good.

Input

The first line contains one integer t (1t5000) — the number of test cases.

The first line of each test case contains one integer n (1n2105) — the length of array a.

The second line of each test case contains n integers a1,a2,,an (1ai109).

It's guaranteed that the sum of n doesn't exceed 2105.

Output

For each test case, print the number of good subarrays of array a.

Example
Input
Copy
3
+4
+2 4 1 3
+5
+6 9 1 9 6
+2
+13 37
+
Output
Copy
10
+12
+3
+
Note

In the first test case, it can be proven that any subarray of a is good. For example, subarray [a2,a3,a4] is good since it contains only three elements and:

  • d((a2,2),(a4,4))=|43|+|24|=3<d((a2,2),(a3,3))+d((a3,3),(a4,4))=3+1+2+1=7;
  • d((a2,2),(a3,3))<d((a2,2),(a4,4))+d((a4,4),(a3,3));
  • d((a3,3),(a4,4))<d((a3,3),(a2,2))+d((a2,2),(a4,4));

In the second test case, for example, subarray [a1,a2,a3,a4] is not good, since it contains a bad triple (a1,1), (a2,2), (a4,4):

  • d((a1,1),(a4,4))=|69|+|14|=6;
  • d((a1,1),(a2,2))=|69|+|12|=4;
  • d((a2,2),(a4,4))=|99|+|24|=2;

So, d((a1,1),(a4,4))=d((a1,1),(a2,2))+d((a2,2),(a4,4)).

+
+ +
+
+ + +
+ +
D. Excellent Arrays
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's call an integer array a1,a2,,angood if aii for each i.

Let F(a) be the number of pairs (i,j) (1i<jn) such that ai+aj=i+j.

Let's say that an array a1,a2,,an is excellent if:

  • a is good;
  • lair for each i;
  • F(a) is the maximum possible among all good arrays of size n.

Given n, l and r, calculate the number of excellent arrays modulo 109+7.

Input

The first line contains a single integer t (1t1000) — the number of test cases.

The first and only line of each test case contains three integers n, l, and r (2n2105; 109l1; nr109).

It's guaranteed that the sum of n doesn't exceed 2105.

Output

For each test case, print the number of excellent arrays modulo 109+7.

Example
Input
Copy
4
+3 0 3
+4 -3 5
+42 -33 55
+69 -42 146
+
Output
Copy
4
+10
+143922563
+698570404
+
Note

In the first test case, it can be proven that the maximum F(a) among all good arrays a is equal to 2. The excellent arrays are:

  1. [2,1,2];
  2. [0,3,2];
  3. [2,3,2];
  4. [3,0,1].

+
+ +
+
+ + +
+ +
E. Stringforces
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.

You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.

Let fi be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then fi is equal to 0.

The value of a string s is the minimum value among fi for all i from 1 to k.

What is the maximum value the string can have?

Input

The first line contains two integers n and k (1n2105; 1k17) — the length of the string and the number of first Latin letters used.

The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.

Output

Print a single integer — the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.

Examples
Input
Copy
10 2
+a??ab????b
+
Output
Copy
4
+
Input
Copy
9 4
+?????????
+
Output
Copy
2
+
Input
Copy
2 3
+??
+
Output
Copy
0
+
Input
Copy
15 3
+??b?babbc??b?aa
+
Output
Copy
3
+
Input
Copy
4 4
+cabd
+
Output
Copy
1
+
Note

In the first example the question marks can be replaced in the following way: "aaaababbbb". f1=4, f2=4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f1=4, f2=6, however, the minimum of them is still 4.

In the second example one of the possible strings is "aabbccdda".

In the third example at least one letter won't appear in the string, thus, the minimum of values fi is always 0.

+
+ +
+
+ + +
+ +
F. Jumping Around
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is an infinite pond that can be represented with a number line. There are n rocks in the pond, numbered from 1 to n. The i-th rock is located at an integer coordinate ai. The coordinates of the rocks are pairwise distinct. The rocks are numbered in the increasing order of the coordinate, so a1<a2<<an.

A robot frog sits on the rock number s. The frog is programmable. It has a base jumping distance parameter d. There also is a setting for the jumping distance range. If the jumping distance range is set to some integer k, then the frog can jump from some rock to any rock at a distance from dk to d+k inclusive in any direction. The distance between two rocks is an absolute difference between their coordinates.

You are assigned a task to implement a feature for the frog. Given two integers i and k determine if the frog can reach a rock number i from a rock number s performing a sequence of jumps with the jumping distance range set to k. The sequence can be arbitrarily long or empty.

You will be given q testcases for that feature, the j-th testcase consists of two integers i and k. Print "Yes" if the i-th rock is reachable and "No" otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and 'YES"' will be recognized as a positive answer).

Input

The first line contains four integers n, q, s and d (1n,q2105; 1sn; 1d106) — the number of rocks, the number of testcases, the starting rock and the base jumping distance parameter.

The second line contains n integers a1,a2,,an (1ai106) — the coordinates of the rocks. The coordinates of the rocks are pairwise distinct. The rocks are numbered in the increasing order of distance from the land, so a1<a2<<an.

Each of the next q lines contains two integers i and k (1in; 1k106) — the parameters to the testcase.

Output

For each of the testcases print an answer. If there is a sequence of jumps from a rock number s to a rock number i with the jumping distance range set to k, then print "Yes". Otherwise, print "No".

Examples
Input
Copy
7 4 4 5
+1 5 10 13 20 22 28
+4 1
+7 2
+7 3
+3 2
+
Output
Copy
Yes
+No
+Yes
+Yes
+
Input
Copy
10 8 6 11
+1 2 4 7 8 9 11 13 19 20
+2 13
+5 8
+8 1
+6 15
+1 15
+2 7
+7 6
+8 9
+
Output
Copy
Yes
+Yes
+No
+Yes
+Yes
+Yes
+Yes
+Yes
+
Input
Copy
6 9 6 6
+1 2 4 9 18 19
+2 17
+1 18
+5 4
+2 11
+5 17
+6 8
+4 3
+3 3
+6 6
+
Output
Copy
Yes
+Yes
+Yes
+Yes
+Yes
+Yes
+No
+No
+Yes
+
Input
Copy
4 1 1 10
+1 8 10 19
+2 1
+
Output
Copy
Yes
+
Note

Explanation of the first example:

In the first testcase the destination rock is the same as the starting rock, thus no jumps are required to reach it.

In the second testcase the frog can jump any distance in the range [52;5+2]. Thus, it can reach rock number 5 (by jumping 7 to the right) and rock number 3 (by jumping 3 to the left). From rock number 3 it can reach rock number 2 (by jumping 5 to the left). From rock number 2 it can reach rock number 1 (by jumping 4 to the left). However, there is no way to reach rock number 7.

In the third testcase the frog can jump any distance in the range [53;5+3]. Thus, it can reach rock number 7 by jumping to rock 5 first and to 7 afterwards.

The fourth testcase is shown in the explanation for the second testcase.

+
+ +
+
+ + + +
+
\ No newline at end of file