Submission #69372166
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
// 実質無限大
constexpr long long INF = 2000000000000000000;
// 最小公倍数を求める(ただし大きすぎたらINFを返す)関数
long long my_lcm (long long a, long long b) {
if (a==INF||b==INF) return INF;
long long g = gcd(a,b);
if (1.0*a/g*b>INF) return INF;
return a/g*b;
}
// 組み合わせ計算(nが小さいので簡易版)
long long nCr (int n, int r) {
long long tmp = 1;
for (int i=0; i<r; i++) {
tmp *= (n-i);
tmp /= (i+1);
}
return tmp;
}
/////////////////// メイン ///////////////////
int main () {
//////////////////// 入力 ////////////////////
int n, m;
long long y;
cin >> n >> m >> y;
vector<long long> a(n);
for (int i=0; i<n; i++) {
cin >> a.at(i);
}
//////////////// 出力変数定義 ////////////////
long long result = 0;
//////////////////// 処理 ////////////////////
// 発生するセミの種類をビット管理して全パターンループ
for (int i=0; i<(1<<n); i++) {
// 最小公倍数計算用
long long tmp = 1;
// そうなる回数
long long num = 0;
// 何種類のセミが発生するか
int count = 0;
// セミの種類ごとに最小公倍数と種類数を更新
for (int j=0; j<n; j++) {
if (i&(1<<j)) {
count++;
tmp = my_lcm(tmp,a.at(j));
}
}
// そうなる年数を求める
num = y/tmp;
// 包除原理っぽい感じでがんばる
// 発生するセミの種類数がm未満なら無視
// m以上で、mと偶奇が一致するならComb[種類数,m]倍したものを足す
// m以上で、mと偶奇が異なるならComb[種類数,m]倍したものをを引く
// 途中でlong longをはみ出す場合があるが、最終的に0以上y以下に戻ってくるのでよし
if (count>=m) {
if ((count+m)%2==0) {
result += num*nCr(count,m);
} else {
result -= num*nCr(count,m);
}
}
}
//////////////////// 出力 ////////////////////
cout << result << endl;
//////////////////// 終了 ////////////////////
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Loud Cicada |
| User | wightou |
| Language | C++ 23 (gcc 12.2) |
| Score | 525 |
| Code Size | 2271 Byte |
| Status | AC |
| Exec Time | 471 ms |
| Memory | 3648 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 525 / 525 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt |
| All | 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt, 01-52.txt, 01-53.txt, 01-54.txt, 01-55.txt, 01-56.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00-sample-01.txt | AC | 1 ms | 3504 KiB |
| 00-sample-02.txt | AC | 1 ms | 3564 KiB |
| 00-sample-03.txt | AC | 182 ms | 3428 KiB |
| 01-01.txt | AC | 1 ms | 3416 KiB |
| 01-02.txt | AC | 1 ms | 3460 KiB |
| 01-03.txt | AC | 1 ms | 3456 KiB |
| 01-04.txt | AC | 1 ms | 3456 KiB |
| 01-05.txt | AC | 1 ms | 3560 KiB |
| 01-06.txt | AC | 1 ms | 3452 KiB |
| 01-07.txt | AC | 1 ms | 3500 KiB |
| 01-08.txt | AC | 1 ms | 3496 KiB |
| 01-09.txt | AC | 1 ms | 3500 KiB |
| 01-10.txt | AC | 1 ms | 3564 KiB |
| 01-11.txt | AC | 1 ms | 3460 KiB |
| 01-12.txt | AC | 1 ms | 3436 KiB |
| 01-13.txt | AC | 1 ms | 3368 KiB |
| 01-14.txt | AC | 1 ms | 3460 KiB |
| 01-15.txt | AC | 440 ms | 3460 KiB |
| 01-16.txt | AC | 471 ms | 3564 KiB |
| 01-17.txt | AC | 470 ms | 3480 KiB |
| 01-18.txt | AC | 283 ms | 3456 KiB |
| 01-19.txt | AC | 261 ms | 3452 KiB |
| 01-20.txt | AC | 216 ms | 3480 KiB |
| 01-21.txt | AC | 1 ms | 3456 KiB |
| 01-22.txt | AC | 1 ms | 3412 KiB |
| 01-23.txt | AC | 1 ms | 3408 KiB |
| 01-24.txt | AC | 1 ms | 3456 KiB |
| 01-25.txt | AC | 99 ms | 3648 KiB |
| 01-26.txt | AC | 1 ms | 3560 KiB |
| 01-27.txt | AC | 1 ms | 3456 KiB |
| 01-28.txt | AC | 1 ms | 3560 KiB |
| 01-29.txt | AC | 39 ms | 3480 KiB |
| 01-30.txt | AC | 1 ms | 3372 KiB |
| 01-31.txt | AC | 1 ms | 3408 KiB |
| 01-32.txt | AC | 1 ms | 3468 KiB |
| 01-33.txt | AC | 203 ms | 3416 KiB |
| 01-34.txt | AC | 176 ms | 3560 KiB |
| 01-35.txt | AC | 165 ms | 3440 KiB |
| 01-36.txt | AC | 180 ms | 3644 KiB |
| 01-37.txt | AC | 4 ms | 3440 KiB |
| 01-38.txt | AC | 8 ms | 3504 KiB |
| 01-39.txt | AC | 357 ms | 3432 KiB |
| 01-40.txt | AC | 358 ms | 3504 KiB |
| 01-41.txt | AC | 361 ms | 3372 KiB |
| 01-42.txt | AC | 358 ms | 3452 KiB |
| 01-43.txt | AC | 353 ms | 3504 KiB |
| 01-44.txt | AC | 177 ms | 3448 KiB |
| 01-45.txt | AC | 175 ms | 3428 KiB |
| 01-46.txt | AC | 408 ms | 3332 KiB |
| 01-47.txt | AC | 382 ms | 3492 KiB |
| 01-48.txt | AC | 305 ms | 3412 KiB |
| 01-49.txt | AC | 336 ms | 3480 KiB |
| 01-50.txt | AC | 3 ms | 3492 KiB |
| 01-51.txt | AC | 1 ms | 3372 KiB |
| 01-52.txt | AC | 157 ms | 3372 KiB |
| 01-53.txt | AC | 157 ms | 3452 KiB |
| 01-54.txt | AC | 161 ms | 3484 KiB |
| 01-55.txt | AC | 161 ms | 3420 KiB |
| 01-56.txt | AC | 155 ms | 3408 KiB |