結果
| 問題 |
No.1946 ロッカーの問題
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2022-05-20 21:52:03 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 3,000 ms |
| コード長 | 9,905 bytes |
| コンパイル時間 | 3,827 ms |
| コンパイル使用メモリ | 124,196 KB |
| 最終ジャッジ日時 | 2025-01-29 10:30:02 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
#include <cstdio>
#include <string>
namespace nachia{
const unsigned int INPUT_BUF_SIZE = 2 << 20;
const unsigned int OUTPUT_BUF_SIZE = 1 << 20;
char input_buf[INPUT_BUF_SIZE];
struct input_buf_init{
input_buf_init(){
input_buf[fread(input_buf, 1, INPUT_BUF_SIZE-1, stdin)] = '\0';
}
} input_buf_init_instance;
struct input_buf_iterator{
private:
unsigned int p = 0;
public:
static bool is_whitespace(char ch){
switch(ch){
case ' ': case '\n': case '\r': case '\t': return true;
}
return false;
}
void skip_whitespace(){
while(is_whitespace(input_buf[p])) p++;
}
unsigned int next_uint(){
skip_whitespace();
unsigned int buf = 0;
while(true){
char tmp = input_buf[p];
if('9' < tmp || tmp < '0') break;
buf = buf * 10 + (tmp - '0');
p++;
}
return buf;
}
int next_int(){
skip_whitespace();
if(input_buf[p] == '-'){
p++;
return (int)(-next_uint());
}
return (int)next_uint();
}
unsigned long long next_ulong(){
skip_whitespace();
unsigned long long buf = 0;
while(true){
char tmp = input_buf[p];
if('9' < tmp || tmp < '0') break;
buf = buf * 10 + (tmp - '0');
p++;
}
return buf;
}
long long next_long(){
skip_whitespace();
if(input_buf[p] == '-'){
p++;
return (long long)(-next_ulong());
}
return (long long)next_ulong();
}
char next_char(){
skip_whitespace();
return input_buf[p++];
}
std::string next_token(){
skip_whitespace();
std::string buf;
while(true){
char ch = input_buf[p];
if(is_whitespace(ch)) break;
buf.push_back(ch);
p++;
}
return buf;
}
};
char output_buf[OUTPUT_BUF_SIZE] = {};
struct fastoutput_table{
char dig3lz[1000][4];
char dig3nlz[1000][4];
fastoutput_table(){
for(unsigned int d=0; d<1000; d++){
unsigned int x = d;
unsigned int i = 0;
dig3lz[d][i++] = ('0' + x / 100 % 10);
dig3lz[d][i++] = ('0' + x / 10 % 10);
dig3lz[d][i++] = ('0' + x / 1 % 10);
dig3lz[d][i++] = '\0';
}
for(unsigned int d=0; d<1000; d++){
unsigned int x = d;
unsigned int i = 0;
if(x >= 100) dig3nlz[d][i++] = ('0' + x / 100 % 10);
if(x >= 10) dig3nlz[d][i++] = ('0' + x / 10 % 10);
if(x >= 1) dig3nlz[d][i++] = ('0' + x / 1 % 10);
dig3nlz[d][i++] = '\0';
}
}
} fastoutput_table_inst;
struct output_buf_iterator{
unsigned int p = 0;
void next_char(char c){
output_buf[p++] = c;
}
void next_eoln(){
next_char('\n');
}
void next_cstr(char* s){
int i = 0;
while(s[i]) next_char(s[i++]);
}
void next_dig9(unsigned int x){
unsigned int y;
y = x / 1'000'000; x -= y * 1'000'000;
next_cstr(fastoutput_table_inst.dig3lz [y]);
y = x / 1'000; x -= y * 1'000;
next_cstr(fastoutput_table_inst.dig3lz [y]);
y = x;
next_cstr(fastoutput_table_inst.dig3lz [y]);
}
void next_uint(unsigned int x){
unsigned int y = 0;
if(x >= 1'000'000'000){
y = x / 1'000'000'000; x -= y * 1'000'000'000;
next_cstr(fastoutput_table_inst.dig3nlz[y]);
next_dig9(x);
}
else if(x >= 1'000'000){
y = x / 1'000'000; x -= y * 1'000'000;
next_cstr(fastoutput_table_inst.dig3nlz[y]);
y = x / 1'000; x -= y * 1'000;
next_cstr(fastoutput_table_inst.dig3lz [y]);
next_cstr(fastoutput_table_inst.dig3lz [x]);
}
else if(x >= 1'000){
y = x / 1'000; x -= y * 1'000;
next_cstr(fastoutput_table_inst.dig3nlz[y]);
next_cstr(fastoutput_table_inst.dig3lz [x]);
}
else if(x >= 1){
next_cstr(fastoutput_table_inst.dig3nlz[x]);
}
else{
next_char('0');
}
}
void next_ulong(unsigned long long x){
unsigned int y = 0;
if(x >= 1'000'000'000'000'000'000){
y = x / 1'000'000'000'000'000'000; x -= (unsigned long long)y * 1'000'000'000'000'000'000;
next_uint(y);
y = x / 1'000'000'000; x -= (unsigned long long)y * 1'000'000'000;
next_dig9(y);
next_dig9(x);
}
else if(x >= 1'000'000'000){
y = x / 1'000'000'000; x -= (unsigned long long)y * 1'000'000'000;
next_uint(y);
next_dig9(x);
}
else{
next_uint(x);
}
}
void write_to_file(){
fwrite(output_buf, p, 1, stdout);
}
};
} // namespace nachia
#include <vector>
#include <algorithm>
#include <cassert>
#include <iostream>
namespace nachia{
namespace prime_sieve_explicit_internal{
std::vector<bool> isprime = { false }; // a[x] := isprime(2x+1)
void CalcIsPrime(int z){
if((int)isprime.size() *2+1 < z+1){
int new_z = isprime.size();
while(new_z*2+1 < z+1) new_z *= 2;
z = new_z-1;
isprime.resize(z+1, true);
for(int i=1; i*(i+1)*2<=z; i++) if(isprime[i]){
for(int j=i*(i+1)*2; j<=z; j+=i*2+1) isprime[j] = false;
}
}
}
std::vector<int> prime_list = {2};
int prime_list_max = 0;
void CalcPrimeList(int z){
while((int)prime_list.size() < z){
if((int)isprime.size() <= prime_list_max + 1) CalcIsPrime(prime_list_max + 1);
for(int p=prime_list_max+1; p<(int)isprime.size(); p++){
if(isprime[p]) prime_list.push_back(p*2+1);
}
prime_list_max = isprime.size() - 1;
}
}
void CalcPrimeListUntil(int z){
if(prime_list_max < z){
CalcIsPrime(z);
for(int p=prime_list_max+1; p<(int)isprime.size(); p++){
if(isprime[p]) prime_list.push_back(p*2+1);
}
prime_list_max = isprime.size() - 1;
}
}
}
bool IsprimeExplicit(int n){
using namespace prime_sieve_explicit_internal;
if(n == 2) return true;
if(n % 2 == 0) return false;
CalcIsPrime(n);
return isprime[(n-1)/2];
}
int NthPrimeExplicit(int n){
using namespace prime_sieve_explicit_internal;
CalcPrimeList(n);
return prime_list[n];
}
int PrimeCountingExplicit(int n){
using namespace prime_sieve_explicit_internal;
if(n < 2) return 0;
CalcPrimeListUntil(n);
auto res = ::std::upper_bound(prime_list.begin(), prime_list.end(), n) - prime_list.begin();
return (int)res;
}
// [l, r)
::std::vector<bool> SegmentedSieveExplicit(long long l, long long r){
assert(0 <= l); assert(l <= r);
long long d = r - l;
if(d == 0) return {};
::std::vector<bool> res(d, true);
for(long long p=2; p*p<=r; p++) if(IsprimeExplicit(p)){
long long il = (l+p-1)/p, ir = (r+p-1)/p;
if(il <= p) il = p;
for(long long i=il; i<ir; i++) res[i*p-l] = false;
}
if(l < 2) for(long long p=l; p<2 && p<r; p++) res[l-p] = false;
return res;
}
}
namespace nachia{
template<class Elem>
void DivisorZeta(std::vector<Elem>& a){
using namespace prime_sieve_explicit_internal;
int n = a.size() - 1;
for(int d=2; d<=n; d++) if(IsprimeExplicit(d)) for(int i=1; i*d<=n; i++) a[i*d] += a[i];
}
template<class Elem>
void DivisorInvZeta(std::vector<Elem>& a){
using namespace prime_sieve_explicit_internal;
int n = a.size() - 1;
for(int d=2; d<=n; d++) if(IsprimeExplicit(d)) for(int i=n/d; i>=1; i--) a[i] += a[i*d];
}
template<class Elem>
void DivisorMobius(std::vector<Elem>& a){
using namespace prime_sieve_explicit_internal;
int n = a.size() - 1;
for(int d=2; d<=n; d++) if(IsprimeExplicit(d)) for(int i=n/d; i>=1; i--) a[i*d] -= a[i];
}
template<class Elem>
void DivisorInvMobius(std::vector<Elem>& a){
using namespace prime_sieve_explicit_internal;
int n = a.size() - 1;
for(int d=2; d<=n; d++) if(IsprimeExplicit(d)) for(int i=1; i*d<=n; i++) a[i] -= a[i*d];
}
template<class Elem>
std::vector<Elem> GcdConvolution(std::vector<Elem> a, std::vector<Elem> b){
assert(a.size() == b.size());
assert(1 <= a.size());
DivisorInvZeta(a);
DivisorInvZeta(b);
for(int i=1; i<(int)a.size(); i++) a[i] *= b[i];
DivisorInvMobius(a);
return a;
}
template<class Elem>
std::vector<Elem> LcmConvolution(std::vector<Elem> a, std::vector<Elem> b){
assert(a.size() == b.size());
assert(1 <= a.size());
DivisorZeta(a);
DivisorZeta(b);
for(int i=1; i<(int)a.size(); i++) a[i] *= b[i];
DivisorMobius(a);
return a;
}
}
#include <string>
#include <array>
#include <atcoder/modint>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;
using modint = atcoder::static_modint<998244353>;
int main(){
nachia::input_buf_iterator iitr;
nachia::output_buf_iterator oitr;
int N = iitr.next_uint();
int M = iitr.next_uint();
vector<int> A(N+1, 0);
rep(i,M){ A[iitr.next_uint()] = 1; }
nachia::DivisorInvMobius(A);
int ans = 0;
rep(i,N) if(A[i+1] % 2 == 0) ans++;
oitr.next_uint(ans);
oitr.next_eoln();
oitr.write_to_file();
return 0;
}
struct ios_do_not_sync{
ios_do_not_sync(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
}
} ios_do_not_sync_instance;
Nachia