Skip to content

Commit f173a1c

Browse files
committed
変更
1 parent 1444e62 commit f173a1c

8 files changed

Lines changed: 444 additions & 103 deletions

File tree

ahc/getenv.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <string>
2+
3+
template <typename T> T env(const std::string &name, T def) {
4+
char *val = getenv(name.c_str());
5+
if (val == nullptr)
6+
return def;
7+
return static_cast<T>(atoll(val));
8+
}

graph_tree/reroot.hpp

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,68 @@
11
#pragma once
2-
#include<vector>
2+
#include <vector>
33

44
/**
55
* @brief 全方位木DP
66
*/
77

8-
template<typename T,typename F,typename Fix>
9-
struct reroot{
10-
std::vector<std::vector<long long>>g;
11-
std::vector<int>p_list;
12-
std::vector<T>p_table;
13-
std::vector<bool>p_checked;
14-
std::vector<map<int,T>>table;
15-
std::vector<T>ans;
8+
template <typename T, typename F, typename Fix> struct reroot {
9+
std::vector<std::vector<long long>> g;
10+
std::vector<int> p_list;
11+
std::vector<T> p_table;
12+
std::vector<bool> p_checked;
13+
std::vector<std::map<int, T>> table;
14+
std::vector<T> ans;
1615
T e;
1716
F f;
1817
Fix fix;
19-
reroot(const std::vector<std::vector<long long>>& g,T e,F f=F(),Fix fix=Fix()):g(g),e(e),f(f),fix(fix){
20-
int n=g.size();
21-
p_list.resize(n,-1);
22-
p_checked.resize(n,0);
18+
reroot(const std::vector<std::vector<long long>>& g,
19+
T e,
20+
F f = F(),
21+
Fix fix = Fix())
22+
: g(g), e(e), f(f), fix(fix) {
23+
int n = g.size();
24+
p_list.resize(n, -1);
25+
p_checked.resize(n, 0);
2326
table.resize(n);
24-
p_table.resize(n,e);
25-
ans.resize(n,e);
26-
dfs1(0,-1);
27-
for(int i=0;i<n;++i)ans[i]=dfs2(i,-1);
27+
p_table.resize(n, e);
28+
ans.resize(n, e);
29+
dfs1(0, -1);
30+
for (int i = 0; i < n; ++i) ans[i] = dfs2(i, -1);
2831
}
29-
T dfs1(int n,int p){
30-
p_list[n]=p;
31-
T tmp1=e,tmp2=e;
32-
std::vector<T>tmp(g[n].size());
33-
rep(i,g[n].size()){
34-
int t=g[n][i];
35-
if(t==p)continue;
36-
table[n][t]=tmp1;
37-
tmp1=f(tmp1,tmp[i]=dfs1(t,n));
32+
T dfs1(int n, int p) {
33+
p_list[n] = p;
34+
T tmp1 = e, tmp2 = e;
35+
std::vector<T> tmp(g[n].size());
36+
for (int i = 0; i < g[n].size(); ++i) {
37+
int t = g[n][i];
38+
if (t == p) continue;
39+
table[n][t] = tmp1;
40+
tmp1 = f(tmp1, tmp[i] = dfs1(t, n));
3841
}
39-
for(int i=g[n].size()-1;i>=0;--i){
40-
int t=g[n][i];
41-
if(t==p)continue;
42-
table[n][t]=f(table[n][t],tmp2);
43-
tmp2=f(tmp[i],tmp2);
42+
for (int i = g[n].size() - 1; i >= 0; --i) {
43+
int t = g[n][i];
44+
if (t == p) continue;
45+
table[n][t] = f(table[n][t], tmp2);
46+
tmp2 = f(tmp[i], tmp2);
4447
}
45-
return fix(table[n][p]=tmp1,n,p);
48+
return fix(table[n][p] = tmp1, n, p);
4649
}
47-
T dfs2(int n,int p){
48-
if(n==-1){
50+
T dfs2(int n, int p) {
51+
if (n == -1) {
4952
return e;
5053
}
51-
if(!p_checked[n]){
52-
p_checked[n]=1;
53-
p_table[n]=dfs2(p_list[n],n);
54+
if (!p_checked[n]) {
55+
p_checked[n] = 1;
56+
p_table[n] = dfs2(p_list[n], n);
5457
}
55-
if(p==-1){
56-
return f(table[n][p_list[n]],p_table[n]);
57-
}else{
58-
if(p_list[n]==-1)return fix(table[n][p],n,p);
59-
else return fix(f(table[n][p],p_table[n]),n,p);
58+
if (p == -1) {
59+
return f(table[n][p_list[n]], p_table[n]);
60+
} else {
61+
if (p_list[n] == -1)
62+
return fix(table[n][p], n, p);
63+
else
64+
return fix(f(table[n][p], p_table[n]), n, p);
6065
}
6166
}
62-
vector<T>query(){
63-
return ans;
64-
}
67+
std::vector<T> query() { return ans; }
6568
};

math/poly.hpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ pair<poly,poly> divmod(const poly&a,const poly& b){
118118
return make_pair(c,pre(a-c*b,(int)b.size()-1));
119119
}
120120

121+
// 多項式の gcd(最高次係数を 1 に正規化)
122+
poly poly_gcd(poly a, poly b){
123+
a=shrink(a); b=shrink(b);
124+
while(!b.empty()){
125+
auto r=divmod(a,b).second;
126+
a=b; b=shrink(r);
127+
}
128+
if(a.empty()) return a;
129+
mint inv_lead=a.back().inv();
130+
for(auto &v:a) v*=inv_lead;
131+
return a;
132+
}
133+
121134
poly multipoint_evalution(const poly&a,const poly&b){
122135
int n=b.size();
123136
vector<poly>v(n*2);
@@ -159,4 +172,45 @@ vector<mint> composition(vector<mint>f,vector<mint>g){
159172
g_pow2.resize(n);
160173
}
161174
return res;
162-
}
175+
}
176+
vector<mint> shift(vector<mint>f,int c){
177+
const int n=f.size();
178+
vector<mint> g(n,0);
179+
for(int i=0;i<n;++i)f[i]*=fact(i);
180+
for(int i=0;i<n;++i)g[i]=mint(c).pow(i)*fact_inv(i);
181+
reverse(begin(g),end(g));
182+
f*=g;
183+
f =vector<mint>{f.begin()+n-1,f.end()};
184+
for(int i=0;i<n;++i)f[i]*=fact_inv(i);
185+
return f;
186+
}
187+
188+
poly even_part(const poly& a){
189+
poly res;
190+
res.reserve((a.size()+1)/2);
191+
for(int i=0;i<a.size();i+=2)res.emplace_back(a[i]);
192+
return shrink(res);
193+
}
194+
poly odd_part(const poly& a){
195+
poly res;
196+
res.reserve(a.size()/2);
197+
for(int i=1;i<a.size();i+=2)res.emplace_back(a[i]);
198+
return shrink(res);
199+
}
200+
201+
// bostan-mori: P/Q の級数展開における x^k の係数を返す (Q[0] ≠ 0 を仮定)
202+
mint bostan_mori(poly p, poly q, long long k){
203+
p=shrink(p); q=shrink(q);
204+
assert(!q.empty() && q[0].val()!=0);
205+
while(k){
206+
poly q_neg(q.size());
207+
rep(i,0,q.size()) q_neg[i]=(i&1)?-q[i]:q[i];
208+
poly r=p*q_neg;
209+
poly s=q*q_neg;
210+
if(k&1) p=odd_part(r);
211+
else p=even_part(r);
212+
q=even_part(s);
213+
k>>=1;
214+
}
215+
return p.empty()?mint(0):p[0]/q[0];
216+
}

math/prime_list.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @brief 素数列挙
99
*/
1010

11-
std::bitset<10'000'001> p;
11+
std::bitset<10'101'011> p;
1212
std::vector<long long> prime_list(int n) {
1313
p.set();
1414
p[0]=0;

util/bits/stdc++.h

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// C++ includes used for precompiling -*- C++ -*-
2+
3+
// Copyright (C) 2003-2021 Free Software Foundation, Inc.
4+
//
5+
// This file is part of the GNU ISO C++ Library. This library is free
6+
// software; you can redistribute it and/or modify it under the
7+
// terms of the GNU General Public License as published by the
8+
// Free Software Foundation; either version 3, or (at your option)
9+
// any later version.
10+
11+
// This library is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
16+
// Under Section 7 of GPL version 3, you are granted additional
17+
// permissions described in the GCC Runtime Library Exception, version
18+
// 3.1, as published by the Free Software Foundation.
19+
20+
// You should have received a copy of the GNU General Public License and
21+
// a copy of the GCC Runtime Library Exception along with this program;
22+
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23+
// <http://www.gnu.org/licenses/>.
24+
25+
/** @file stdc++.h
26+
* This is an implementation file for a precompiled header.
27+
*/
28+
29+
// 17.4.1.2 Headers
30+
31+
// C
32+
#ifndef _GLIBCXX_NO_ASSERT
33+
#include <cassert>
34+
#endif
35+
#include <cctype>
36+
#include <cerrno>
37+
#include <cfloat>
38+
#include <ciso646>
39+
#include <climits>
40+
#include <clocale>
41+
#include <cmath>
42+
#include <csetjmp>
43+
#include <csignal>
44+
#include <cstdarg>
45+
#include <cstddef>
46+
#include <cstdio>
47+
#include <cstdlib>
48+
#include <cstring>
49+
#include <ctime>
50+
#include <cwchar>
51+
#include <cwctype>
52+
53+
// #if __cplusplus >= 201103L
54+
// #include <ccomplex>
55+
// #include <cfenv>
56+
// #include <cinttypes>
57+
// #include <cstdalign>
58+
// #include <cstdbool>
59+
// #include <cstdint>
60+
// #include <ctgmath>
61+
// #include <cuchar>
62+
// #endif
63+
64+
// C++
65+
#include <algorithm>
66+
#include <bitset>
67+
#include <complex>
68+
#include <deque>
69+
#include <exception>
70+
#include <fstream>
71+
// #include <functional>
72+
#include <iomanip>
73+
#include <ios>
74+
#include <iosfwd>
75+
#include <iostream>
76+
#include <istream>
77+
#include <iterator>
78+
#include <limits>
79+
#include <list>
80+
#include <locale>
81+
#include <map>
82+
#include <memory>
83+
#include <new>
84+
#include <numeric>
85+
#include <ostream>
86+
// #include <queue>
87+
#include <set>
88+
#include <sstream>
89+
#include <stack>
90+
#include <stdexcept>
91+
#include <streambuf>
92+
#include <string>
93+
#include <typeinfo>
94+
#include <utility>
95+
#include <valarray>
96+
#include <vector>
97+
98+
#if __cplusplus >= 201103L
99+
#include <array>
100+
#include <atomic>
101+
#include <chrono>
102+
#include <codecvt>
103+
#include <condition_variable>
104+
#include <forward_list>
105+
#include <future>
106+
#include <initializer_list>
107+
#include <mutex>
108+
#include <random>
109+
#include <ratio>
110+
#include <regex>
111+
#include <scoped_allocator>
112+
#include <system_error>
113+
#include <thread>
114+
#include <tuple>
115+
#include <typeindex>
116+
#include <type_traits>
117+
#include <unordered_map>
118+
#include <unordered_set>
119+
#endif
120+
121+
#if __cplusplus >= 201402L
122+
#include <shared_mutex>
123+
#endif
124+
125+
#if __cplusplus >= 201703L
126+
#include <any>
127+
#include <charconv>
128+
// #include <execution>
129+
#include <filesystem>
130+
#include <optional>
131+
#include <memory_resource>
132+
#include <string_view>
133+
#include <variant>
134+
#endif
135+
136+
#if __cplusplus > 201703L
137+
#include <barrier>
138+
#include <bit>
139+
#include <compare>
140+
#include <concepts>
141+
#if __cpp_impl_coroutine
142+
#include <coroutine>
143+
#endif
144+
#include <latch>
145+
#include <numbers>
146+
#include <ranges>
147+
#include <span>
148+
#include <stop_token>
149+
#include <semaphore>
150+
#include <source_location>
151+
#include <syncstream>
152+
#include <version>
153+
#endif

util/getenv.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <cassert>
2+
#include <charconv>
3+
#include <cstdlib>
4+
#include <cstring>
5+
#include <string>
6+
7+
template <typename T> T env(const std::string& s, T def = T()) {
8+
char* p = getenv(s.c_str());
9+
if (p == nullptr) return def;
10+
T res;
11+
std::from_chars(p, p + strlen(p), res);
12+
return res;
13+
}

0 commit comments

Comments
 (0)