結果
| 問題 |
No.2620 Sieve of Coins
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2024-01-15 21:24:51 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 90 ms / 2,000 ms |
| コード長 | 2,015 bytes |
| コンパイル時間 | 1,207 ms |
| コンパイル使用メモリ | 86,056 KB |
| 最終ジャッジ日時 | 2025-02-18 20:13:49 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 53 |
ソースコード
#define NDEBUG
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(i64 i=0; i<(i64)(n); i++)
#define repr(i,n) for(i64 i=(i64)(n)-1; i>=0; i--)
int main(){
i64 L, N; cin >> L >> N;
vector<i64> A(N); rep(i,N) cin >> A[i];
vector<vector<i64>> pow23(1);
pow23[0].push_back(1);
while(pow23.back()[0] * 3 <= L){
i64 l = pow23.back()[0];
pow23.emplace_back(1);
pow23.back()[0] = l * 3;
}
for(auto& a : pow23) while(a.back() * 2 <= L) a.push_back(a.back() * 2);
int H = (int)max(pow23.size(), pow23[0].size());
vector<vector<int>> coins(H, vector<int>(H));
for(i64 a : A){
int p2 = 0; while(a%2 == 0){ p2++; a /= 2; }
int p3 = 0; while(a%3 == 0){ p3++; a /= 3; }
coins[p3][p2] = 1;
}
vector<int> nxrow(H);
rep(y,H){
rep(x,H) coins[y][x] ^= nxrow[x];
rep(x,H) if(coins[y][x]){
for(int xx=x+1; xx<H; xx++) coins[y][xx] ^= 1;
for(int xx=x; xx<H; xx++) nxrow[xx] ^= 1;
}
}
i64 sz = 1;
while((sz+1)*(sz+1) <= L) sz++;
vector<i64> Q(sz+1);
rep(y,H) rep(x,H) if(coins[y][x]) if(y<(int)pow23.size()) if(x<(int)pow23[y].size()){
i64 mp = pow23[y][x];
i64 smallL = L / mp;
for(i64 i=1; i*i<=smallL; i++) if(i%2 != 0 && i%3 != 0){
Q[i] += smallL / (i * i);
Q[i] -= smallL / (i * i * 2);
Q[i] -= smallL / (i * i * 3);
Q[i] += smallL / (i * i * 6);
}
}
vector<int> sieve(sz+1, 1);
sieve[0] = sieve[1] = 0;
vector<int> mobius(sz+1);
mobius[1] = 1;
for(int p=2; p<=sz; p++) if(sieve[p]){
for(int q=2; p*q<=sz; q++) sieve[p*q] = 0;
for(int q=sz/p; q>=1; q--) mobius[p*q] = -mobius[q];
}
i64 ans = 0;
for(int i=1; i<=sz; i++) ans += Q[i] * mobius[i];
cout << ans << endl;
return 0;
}
Nachia