Codeforces & Atcoder & 牛客训练补题
Codeforces & Atcoder & 牛客训练补题
为了准备下 EC-final 开个贴
CF1767C
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int mo = 998244353;
int a[111][111];
ll f[111][111];
void solve() {
int n;
cin >> n;
for(int i = 1; i <= n; i ++) {
for(int j = i; j <= n; j ++) {
cin >> a[i][j];
}
}
f[1][1] = 2;
ll ans = 0;
for(int r = 1; r <= n; r ++) {
for(int l = 1; l <= r; l ++) {
int flag = 0;
for(int i = 1; i < l; i ++) {
if(a[i][r] == 1)flag = 1;
}
for(int i = l; i <= r; i ++) {
if(a[i][r] == 2)flag = 1;
}
if(flag)f[l][r] = 0;
f[l][r + 1] = (f[l][r + 1] + f[l][r]) % mo;
f[r + 1][r + 1] = (f[r + 1][r + 1] + f[l][r]) % mo;
}
}
for(int i = 1; i <= n; i ++) {
ans = (ans + f[i][n]) % mo;
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;T = 1;
while(T --) {
solve();
}
}
/*
3
1 2 2
1 2
1
*/
ABC283F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
int n;
int p[maxn];
int c1[maxn],c2[maxn];
void add(int x,int y){
for(;x <= n;x += (x & (-x)))c1[x] = min(c1[x],y);
}
int ask(int x){
int ans = 1e9;
for(;x > 0;x -= (x & (-x)))ans = min(ans,c1[x]);
return ans;
}
void add1(int x,int y){
for(;x <= n;x += (x & (-x)))c2[x] = max(c2[x],y);
}
int ask1(int x){
int ans = -1e9;
for(;x > 0;x -= (x & (-x)))ans = max(ans,c2[x]);
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1;i <= n;i ++){
cin >> p[i];c1[i] = 1e9;c2[i] = -1e9;
}
std::vector<int> ans(n + 1,1e9);
for(int i = 1;i <= n;i ++){
ans[i] = min(ans[i], p[i] + i - ask1(p[i]));
ans[i] = min(ans[i],ask(n - p[i] + 1) - p[i] + i);
add1(p[i],p[i] + i);
add(n - p[i] + 1,p[i] - i);
}
for(int i = 1;i <= n;i ++)c1[i] = 1e9,c2[i] = -1e9;
for(int i = n;i >= 1;i --){
ans[i] = min(ans[i], ask(n - p[i] + 1) - p[i] - i);
ans[i] = min(ans[i],p[i] - i - ask1(p[i]));
add(n - p[i] + 1,p[i] + i);
add1(p[i],p[i] - i);
}
for(int i = 1;i <= n;i ++)cout << ans[i] << " ";
return 0;
}
// x1 < x2,y1 < y2
// -(x1 + y1) + (x2 + y2)
// x1 > x2,y1 < y2
// x1 - y1 - (x2 - y2)
// x1 > x2,y1 > y2
// x1 + y1 - (x2 + y2)
// x1 < x2,y1 > y2
// -(x1 - y1) + (x2 - y2)
CF1770C
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[210];
int n;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int T;
cin >> T;
while(T --){
cin >> n;
for(int i = 1;i <= n;i ++)cin >> a[i];
sort(a + 1,a + 1 + n);
int pd = 0;
for(int i = 2;i <= n;i ++)if(a[i] == a[i - 1])pd = 1;
for(int j = 2;j <= n;j ++){
std::vector<int> cnt(n + 1,0);
for(int i = 1;i <= n;i ++){
cnt[a[i] % j] ++;
}
int flag = 1;
for(int i = 0;i < j;i ++){
if(cnt[i] <= 1)flag = 0;
}
if(flag)pd = 1;
}
if(pd)cout << "No\n";
else cout << "Yes\n";
}
return 0;
}
// 18 20 22 24 25 26
// 0 2 1 0 1 2
// 1 3 5 7 9 10
// 1 0 2 1 0 1
CF1731E
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;
int phi[maxn];
int vis[maxn];
int p[maxn],tot;
ll f[maxn];
void init(const int n){
phi[1] = 1;
for(int i = 2;i <= n;i ++){
if(!vis[i]){
p[++ tot] = i;
phi[i] = i - 1;
}
for(int j = 1;j <= tot && i * p[j] <= n;j ++){
vis[i * p[j]] = 1;
if(i % p[j] == 0){
phi[i * p[j]] = phi[i] * p[j];
break;
}
phi[i * p[j]] = phi[i] * phi[p[j]];
}
}
f[1] = 0;
for(int i = 2;i <= n;i ++){
f[i] = f[i - 1] + phi[i];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
init(1000001);
int T;
cin >> T;
while(T --){
int n;
ll m;
cin >> n >> m;
ll ans = 0;
for(int i = n;i >= 2;i --){
ll cnt = f[n / i];
cnt = cnt / (i - 1) * (i - 1);
if(cnt > m){
cnt = m / (i - 1) * (i - 1);
}
m -= cnt;
ans += cnt / (i - 1) * i;
}
if(m > 0)cout << -1 << "\n";
else cout << ans << "\n";
}
}
CF1768E
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e6 + 7;
ll fc[maxn],inv[maxn];
ll mo;
ll qp(ll a,ll b){
ll ans = 1,base = a;
while(b != 0){
if(b & 1)ans = ans * base % mo;
base = base * base % mo;
b >>= 1;
}
return ans % mo;
}
ll C(ll n,ll m){
if(m > n)return 0;
return fc[n] * inv[m] % mo * inv[n - m] % mo;
}
void solve() {
int n;
cin >> n >> mo;
fc[0] = 1;
for(int i = 1;i <= 3 * n;i ++)fc[i] = fc[i - 1] * i % mo;
inv[3 * n] = qp(fc[3 * n],mo - 2);
// cout << (inv[3 * n] * fc[3 * n]) % mo << "\n";
for(int i = 3 * n - 1;i >= 0;i --)inv[i] = inv[i + 1] * (i + 1) % mo;
//cout << inv[0] << "\n";
ll cnt0 = 1;
ll cnt1 = (2 * fc[2 * n] % mo - fc[n] + mo) % mo;
//cout << cnt1 << "\n";
ll cnt2 = 2 * C(2 * n,n) % mo * fc[n] % mo * fc[2 * n] % mo;
//cout << cnt2 << "\n";
for(int i = 0;i <= n;i ++){
ll tmp = C(n,i) * C(n,i) % mo * C(2 * n - i,n) % mo * fc[n] % mo * fc[n] % mo * fc[n] % mo;
cnt2 = (cnt2 - tmp + mo) % mo;
}
ll cnt3 = (fc[3 * n] - cnt2 + mo) % mo;
cnt2 = (cnt2 - cnt1 + mo) % mo;
cnt1 = (cnt1 - cnt0 + mo) % mo;
ll ans = (cnt1 + 2 * cnt2 % mo + 3 * cnt3 % mo) % mo;
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;T = 1;
while(T --) {
solve();
}
}
ABC171F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e6 + 7;
const ll mo = 1e9 + 7;
ll fc[maxn],inv[maxn];
ll qp(ll a,ll b){
ll ans = 1,base = a;
while(b != 0){
if(b & 1)ans = ans * base % mo;
base = base * base % mo;
b >>= 1;
}
return ans % mo;
}
ll C(int n,int m){
return fc[n] * inv[m] % mo * inv[n - m] % mo;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int k;string s;
cin >> k >> s;
int n = s.size();
fc[0] = 1;
for(int i = 1;i <= n + k;i ++)fc[i] = fc[i - 1] * i % mo;
inv[n + k] = qp(fc[n + k],mo - 2);
for(int i = n + k - 1;i >= 0;i --)inv[i] = inv[i + 1] * (i + 1) % mo;
ll ans = 0;
for(int i = 1;i <= k + 1;i ++){
ans = (ans + qp(26,i - 1) * C(n + k - i,n - 1) % mo * qp(25,k - i + 1) % mo) % mo;
}
// n + k - i - (n - 1)
cout << ans << "\n";
return 0;
}
CF1775E
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
int n;cin >> n;
std::vector<ll> a(n + 1);
for(int i = 1;i <= n;i ++){
int x;cin >> x;
a[i] = a[i - 1] + x;
}
cout << *max_element(a.begin(),a.end()) - *min_element(a.begin(),a.end()) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;cin >> T;
while(T --){
solve();
}
return 0;
}
CF1775D
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5;
int p[maxn],minp[maxn],tot;
void init(const int n){
for(int i = 2;i <= n;i ++){
if(!minp[i]){
minp[i] = i;
p[++ tot] = i;
}
for(int j = 1;j <= tot && i * p[j] <= n;j ++){
minp[i * p[j]] = p[j];
if(i % p[j] == 0){
break;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int mx = 0;
std::vector<int> a(n + 1);
for(int i = 1;i <= n;i ++){
cin >> a[i];mx = max(mx,a[i]);
}
init(mx);
vector<std::vector<pair<int,int>>> e(n + mx + 1);
for(int i = 1;i <= n;i ++){
for(int x = a[i];x > 1;x /= minp[x]){
e[i].push_back({n + minp[x],0});
e[n + minp[x]].push_back({i,1});
}
}
int s,t;
cin >> s >> t;
deque<int> q;
std::vector<int> pre(n + mx + 1),dis(n + mx + 1);
q.push_back(s);dis[s] = 1;
while(!q.empty()){
int x = q.front();q.pop_front();
for(auto [v,w]:e[x]){
if(dis[v])continue;
dis[v] = dis[x] + w;
pre[v] = x;
if(w)q.push_back(v);
else q.push_front(v);
}
}
if(dis[t] == 0){
cout << -1 << "\n";
}else {
cout << dis[t] << "\n";
std::vector<int> ans;
for(int x = t;x != s;x = pre[x]){
ans.push_back(x);
}ans.push_back(s);
reverse(ans.begin(),ans.end());
for(auto x:ans){
if(x < n)cout << x << " " ;
}cout << "\n";
}
return 0;
}
2023牛客寒假算法基础集训营1-B
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const int mo = 998244353;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,x,y;
cin >> n >> m >> x >> y;
std::vector dp(3 * n + 1, std::vector(x + 1, std::vector<int>(y + 1)));
dp[0][0][0] = 1;
for(int i = 1;i <= n;i ++){
std::vector g(3 * n + 1, std::vector(x + 1, std::vector<int>(y + 1)));
for(int p = 0;p <= 3 * i;p ++){
for(int j = 1;j <= x;j ++){
for(int k = 1;k <= y;k ++){
(dp[p][j][k] += dp[p][j - 1][k - 1]) %= mo;
}
}
}
for(int p = 0;p < 3 * i;p ++){
for(int j = 0;j <= x;j ++){
for(int k = 0;k <= y;k ++){
(g[p + 1][j][k] += dp[p][j][k]) %= mo;
}
}
}
for(int p = 0;p <= 3 * i;p ++){
for(int j = 1;j <= x;j ++){
for(int k = 0;k <= y;k ++){
(dp[p][j][k] += dp[p][j - 1][k]) %= mo;
}
}
}
for(int p = 0;p <= 3 * (i - 1);p ++){
for(int j = 1;j <= x;j ++){
for(int k = 0;k <= y;k ++){
(g[p + 3][j][k] += dp[p][j - 1][k]) %= mo;
}
}
}
for(int p = 0;p <= 3 * i;p ++){
for(int j = x;j >= 1;j --){
for(int k = 0;k <= y;k ++){
(dp[p][j][k] += mo - dp[p][j - 1][k]) %= mo;
}
}
}
for(int p = 0;p <= 3 * i;p ++){
for(int j = 0;j <= x;j ++){
for(int k = 1;k <= y;k ++){
(dp[p][j][k] += dp[p][j][k - 1]) %= mo;
}
}
}
for(int p = 0;p <= 3 * (i - 1);p ++){
for(int j = 0;j <= x;j ++){
for(int k = 1;k <= y;k ++){
(g[p][j][k] += dp[p][j][k - 1]) %= mo;
}
}
}
swap(dp,g);
}
//cerr << 1 <<"\n";
int ans = 0;
for(int i = m;i <= 3 * n;i ++)(ans += dp[i][x][y]) %= mo;
cout << ans << "\n";
return 0;
}
ABC285F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
int n;
int c[27][maxn],cnt[30];
string s;
void add(int t,int x,int y){
for(;x <= n;x += (x & (-x)))c[t][x] += y;
}
int ask(int t,int x){
int ans = 0;
for(;x > 0;x -= (x & (-x)))ans += c[t][x];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
string t = s;
s = '#' + s;
sort(t.begin(),t.end());
for(int i = 1;i <= n;i ++){
add(s[i] - 'a',i,1);
cnt[s[i] - 'a'] ++;
}
int q;cin >> q;
for(int i = 1;i <= q;i ++){
int opt;
cin >> opt;
if(opt == 1){
int x;char c;
cin >> x >> c;
cnt[s[x] - 'a'] --;
add(s[x] - 'a',x,-1);
s[x] = c;
add(s[x] - 'a',x,1);
cnt[s[x] - 'a'] ++;
}else {
int l,r;
cin >> l >> r;
if(s[l] > s[r]){
cout << "No\n";continue;
}
int flag = 0;
int lst = ask(s[l] - 'a',r) - ask(s[l] - 'a',l - 1);
if(lst != ask(s[l] - 'a',l + lst - 1) - ask(s[l] - 'a',l - 1)){
cout << "No\n";continue;
}
lst += l - 1;
for(int j = s[l] - 'a' + 1;j < s[r] - 'a';j ++){
if(ask(j,lst + cnt[j]) - ask(j,lst) != cnt[j]){
//cout << ask(j,lst + cnt[j]) - ask(j,lst) <<"\n";
//cout << j <<" " <<cnt[j]<< "\n";
flag = 1;break;
}
lst += cnt[j];
}
//cout << flag << "\n";
if(flag){cout << "No\n";continue;}
if(ask(s[r] - 'a',r) - ask(s[r] - 'a',lst) != r - lst){
cout << "No\n";continue;
}
cout << "Yes\n";
}
}
return 0;
}
2023牛客寒假算法基础集训营3-H
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;
const ll mo = 1e9 + 7;
const ll inv3 = 333333336;
ll dp[maxn][2][2][2];// h t way/score
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;// 0 - red 1 - edr
ll tmp = inv3;ll ans = 1;
for(int i = 0;i < n;i ++){
(ans *= inv3) %= mo;
(dp[i + 1][0][0][0] += 1) %= mo;
(dp[i + 1][0][0][1] += 1) %= mo;
(dp[i + 1][1][1][0] += 1) %= mo;
for(int j = 0;j <= 1;j ++){
for(int k = 0;k <= 1;k ++){
(dp[i + 1][j][0][0] += dp[i][j][k][0]) %= mo;//new
(dp[i + 1][j][1][0] += dp[i][j][k][0]) %= mo;//new
(dp[i + 1][j][k][0] += dp[i][j][k][0]) %= mo;//last
(dp[i + 1][j][0][1] += dp[i][j][k][0]) %= mo;// new
(dp[i + 1][j][0][1] += dp[i][j][k][1]) %= mo;// last
if(k == 1)(dp[i + 1][j][1][1] += dp[i][j][k][0]) %= mo;
(dp[i + 1][j][1][1] += dp[i][j][k][1]) %= mo;// last
if(j == 1 && k == 1)(dp[i + 1][1][1][1] += dp[i][1][1][0] * 9 % mo) %= mo;
(dp[i + 1][j][k][1] += dp[i][j][k][1] * 10 % mo) %= mo;
}
}
}
ll Ans = 0;
for(int i = 0;i <= 1;i ++)for(int j = 0;j <= 1;j ++)(Ans += dp[n][i][j][1]) %= mo;
ans = ans * Ans % mo;
cout << ans << "\n";
return 0;
}
/*
2
rededr
redred
edredr
red
edr
red....
edr....
edrred
1 + 2 + 1 + 1 + 10 + 9 + 1 / 9
*/
2023牛客寒假算法基础集训营5-G
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;
const int mo = 998244353;
const long double eps = 1e-7;
struct Flow {
static constexpr int INF = 1e9;
int n;
struct Edge {
int to, cap;
Edge(int to, int cap) : to(to), cap(cap) {}
};
vector<Edge> e;
vector<vector<int>> g;
vector<int> cur, h;
Flow(int n) : n(n), g(n) {}
void init(int n) {
for (int i = 0; i < n; i++) g[i].clear();
e.clear();
}
bool bfs(int s, int t) {
h.assign(n, -1);
queue<int> que;
h[s] = 0;
que.push(s);
while (!que.empty()) {
int u = que.front();
que.pop();
for (int i : g[u]) {
int v = e[i].to;
int c = e[i].cap;
if (c > 0 && h[v] == -1) {
h[v] = h[u] + 1;
if (v == t)
return true;
que.push(v);
}
}
}
return false;
}
int dfs(int u, int t, int f) {
if (u == t)
return f;
int r = f;
for (int &i = cur[u]; i < int(g[u].size()); ++i) {
int j = g[u][i];
int v = e[j].to;
int c = e[j].cap;
if (c > 0 && h[v] == h[u] + 1) {
int a = dfs(v, t, std::min(r, c));
e[j].cap -= a;
e[j ^ 1].cap += a;
r -= a;
if (r == 0)
return f;
}
}
return f - r;
}
void addEdge(int u, int v, int c) {
g[u].push_back(e.size());
e.push_back({v, c});
g[v].push_back(e.size());
e.push_back({u, 0});
}
int maxFlow(int s, int t) {
int ans = 0;
while (bfs(s, t)) {
cur.assign(n, 0);
ans += dfs(s, t, INF);
}
return ans;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin >> n;
std::vector<int> a(n),b(n);
for(int i = 0;i < n / 2;i ++){
cin >> a[i];
b[2 * i] = a[i];
}
Flow f(2 * n + 2);
int k = __lg(n);
for(int i = 0;i < n / 2;i ++){
for(int j = 0;j < k;j ++){
int x = a[i] ^ (1 << j);
if(i == n / 2 - 1 || __builtin_popcount(x ^ a[i + 1]) == 1){
f.addEdge(x,2 * i + 1 + n,1);
}
}
}
for(int i = 0;i < n;i ++){
f.addEdge(2 * n,i,1);
f.addEdge(n + i,2 * n + 1,1);
}
f.maxFlow(2 * n,2 * n + 1);
for(int i = 0;i < f.e.size();i += 2){
int u = f.e[i + 1].to;
int v = f.e[i].to;
int c = f.e[i + 1].cap;
if(v >= n && v < 2 * n && c > 0 && u < n){
b[v - n] = u;
}
}
for(int i = 0;i < n;i ++)cout << b[i] << " ";
}
ABC291G
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % P)} {}
constexpr int norm(int x) const {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(P - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, P - 2);
}
constexpr MInt &operator*=(MInt rhs) {
x = 1LL * x * rhs.x % P;
return *this;
}
constexpr MInt &operator+=(MInt rhs) {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
using Z = MInt<P>;
std::vector<int> rev;
template<int P>
std::vector<MInt<P>> roots{0, 1};
template<int P>
constexpr MInt<P> findPrimitiveRoot() {
MInt<P> i = 2;
int k = __builtin_ctz(P - 1);
while (true) {
if (power(i, 1 << (k - 1)) != 1 && power(i, 1 << k) == 1) {
break;
}
i += 1;
}
return i;
}
template<int P>
constexpr MInt<P> primitiveRoot = findPrimitiveRoot<P>();
template<>
constexpr MInt<998244353> primitiveRoot<998244353> {31};
template<int P>
constexpr void dft(std::vector<MInt<P>> &a) {
int n = a.size();
if (int(rev.size()) != n) {
int k = __builtin_ctz(n) - 1;
rev.resize(n);
for (int i = 0; i < n; i++) {
rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
}
}
for (int i = 0; i < n; i++) {
if (rev[i] < i) {
std::swap(a[i], a[rev[i]]);
}
}
if (roots<P>.size() < n) {
int k = __builtin_ctz(roots<P>.size());
roots<P>.resize(n);
while ((1 << k) < n) {
auto e = power(primitiveRoot<P>, 1 << (__builtin_ctz(P - 1) - k - 1));
for (int i = 1 << (k - 1); i < (1 << k); i++) {
roots<P>[2 * i] = roots<P>[i];
roots<P>[2 * i + 1] = roots<P>[i] * e;
}
k++;
}
}
for (int k = 1; k < n; k *= 2) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
MInt<P> u = a[i + j];
MInt<P> v = a[i + j + k] * roots<P>[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}
template<int P>
constexpr void idft(std::vector<MInt<P>> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
MInt<P> inv = (1 - P) / n;
for (int i = 0; i < n; i++) {
a[i] *= inv;
}
}
template<int P = 998244353>
struct Poly {
using Value = MInt<P>;
std::vector<Value> a;
constexpr Poly() : a{} {}
explicit constexpr Poly(int n) : a(n) {}
template<class F>
explicit constexpr Poly(int n, F f) : a(n) {
for (int i = 0; i < n; i++) {
a[i] = f(i);
}
}
explicit constexpr Poly(const std::vector<Value> &a) : a(a) {}
explicit constexpr Poly(const std::initializer_list<Value> &a) : a(a) {}
template<class It>
explicit constexpr Poly(It first, It last) : a(first, last) {}
constexpr int size() const {
return a.size();
}
explicit constexpr operator std::vector<Value>() const {
return a;
}
constexpr Value operator[](int idx) const {
if (idx < size()) {
return a[idx];
} else {
return 0;
}
}
constexpr Value &operator[](int idx) {
return a[idx];
}
constexpr Poly shift(int k) const {
if (k >= 0) {
auto b = a;
b.insert(b.begin(), k, 0);
return Poly(b);
} else if (size() <= -k) {
return Poly();
} else {
return Poly(a.begin() + (-k), a.end());
}
}
constexpr Poly resize(int k) const {
Poly f{a};
f.a.resize(k);
return f;
}
constexpr friend Poly operator+(const Poly &a, const Poly &b) {
std::vector<Value> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] + b[i];
}
return Poly(res);
}
constexpr friend Poly operator-(const Poly &a, const Poly &b) {
std::vector<Value> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++) {
res[i] = a[i] - b[i];
}
return Poly(res);
}
constexpr friend Poly operator-(const Poly &a) {
std::vector<Value> res(a.size());
for (int i = 0; i < int(res.size()); i++) {
res[i] = -a[i];
}
return Poly(res);
}
constexpr friend Poly operator*(Poly a, Poly b) {
if (a.size() == 0 || b.size() == 0) {
return Poly();
}
if (a.size() < b.size()) {
std::swap(a, b);
}
if (b.size() < 128) {
Poly c(a.size() + b.size() - 1);
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
c[i + j] += a[i] * b[j];
}
}
return c;
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot) {
sz *= 2;
}
a.a.resize(sz);
b.a.resize(sz);
dft(a.a);
dft(b.a);
for (int i = 0; i < sz; ++i) {
a.a[i] = a[i] * b[i];
}
idft(a.a);
a.resize(tot);
return a;
}
constexpr friend Poly operator*(Value a, Poly b) {
for (int i = 0; i < int(b.size()); i++) {
b[i] *= a;
}
return b;
}
constexpr friend Poly operator*(Poly a, Value b) {
for (int i = 0; i < int(a.size()); i++) {
a[i] *= b;
}
return a;
}
constexpr Poly &operator+=(Poly b) {
return (*this) = (*this) + b;
}
constexpr Poly &operator-=(Poly b) {
return (*this) = (*this) - b;
}
constexpr Poly &operator*=(Poly b) {
return (*this) = (*this) * b;
}
constexpr Poly &operator*=(Value b) {
return (*this) = (*this) * b;
}
constexpr Poly deriv() const {
if (a.empty()) {
return Poly();
}
std::vector<Value> res(size() - 1);
for (int i = 0; i < size() - 1; ++i) {
res[i] = (i + 1) * a[i + 1];
}
return Poly(res);
}
constexpr Poly integr() const {
std::vector<Value> res(size() + 1);
for (int i = 0; i < size(); ++i) {
res[i + 1] = a[i] / (i + 1);
}
return Poly(res);
}
constexpr Poly inv(int m) const {
Poly x{a[0].inv()};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{2} - resize(k) * x)).resize(k);
}
return x.resize(m);
}
constexpr Poly log(int m) const {
return (deriv() * inv(m)).integr().resize(m);
}
constexpr Poly exp(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x * (Poly{1} - x.log(k) + resize(k))).resize(k);
}
return x.resize(m);
}
constexpr Poly pow(int k, int m) const {
int i = 0;
while (i < size() && a[i] == 0) {
i++;
}
if (i == size() || 1LL * i * k >= m) {
return Poly(m);
}
Value v = a[i];
auto f = shift(-i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k);
}
constexpr Poly sqrt(int m) const {
Poly x{1};
int k = 1;
while (k < m) {
k *= 2;
x = (x + (resize(k) * x.inv(k)).resize(k)) * CInv<2, P>;
}
return x.resize(m);
}
constexpr Poly mulT(Poly b) const {
if (b.size() == 0) {
return Poly();
}
int n = b.size();
std::reverse(b.a.begin(), b.a.end());
return ((*this) * b).shift(-(n - 1));
}
constexpr std::vector<Value> eval(std::vector<Value> x) const {
if (size() == 0) {
return std::vector<Value>(x.size(), 0);
}
const int n = std::max(int(x.size()), size());
std::vector<Poly> q(4 * n);
std::vector<Value> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
q[p] = Poly{1, -x[l]};
} else {
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
if (r - l == 1) {
if (l < int(ans.size())) {
ans[l] = num[0];
}
} else {
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).resize(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).resize(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
constexpr auto begin() const {
return a.begin();
}
constexpr auto end() const {
return a.end();
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin >> n;
vector<int> a(n),b(n),c(n);
for(int i = 0;i < n;i ++)cin >> a[i];
for(int i = 0;i < n;i ++)cin >> b[i];
reverse(b.begin(),b.end());
for(int i = 0;i < 5;i ++){
std::vector<Z> A(2 * n),B(n);
for(int j = 0 ;j < n;j ++){
A[n + j] = A[j] = 1 - (a[j] >> i & 1);
B[j] = 1 - (b[j] >> i & 1);
}
Poly C = Poly(A) * Poly(B);
for(int j = 0;j < n;j ++){
c[j] += ((n - int(C[n + j - 1])) << i);
}
}
int ans = 0;
for(int i = 0;i < n;i ++)ans = max(ans,c[i]);
cout << ans << "\n";
return 0;
}
ARC158C
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mo = 998244353;
const double eps = 1e-8;
const int maxn = 2e5 + 7;
struct node{
int x,y;
bool operator < (const node &a)const {
if(a.x != x)return a.x < x;
else return a.y < y;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
ll ans = 0;
std::vector<ll> a(n);
map<node,ll> mp[20];
for(int i = 0;i < n;i ++){
ll x;cin >> x;
a[i] = x;
while(x != 0){
ans += x % 10;
x /= 10;
}
}
ans *= 2ll * n;
ll base = 1;
for(int i = 1;i <= 16;i ++){
base *= 10;
std::vector<ll> b(n);
for(int j = 0;j < n;j ++){
b[j] = a[j] % base;
}
sort(b.begin(),b.end());
for(int j = 0;j < n;j ++){
//cout << b[j] << " ";
int l = 0,r = n - 1,t = n;
while(l <= r){
int mid = l + r >> 1;
if(b[j] + b[mid] >= base)r = mid - 1,t = mid;
else l = mid + 1;
}
//cout << t <<"\n";
ans -= 1ll * 9 * (n - t);
}//cout << "\n";
}
//cout << Ans << "\n";
cout << ans << "\n";
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下