标签:nlog void 大于 子节点 dde bfs out 连通 memset
#include<bits/stdc++.h>
using namespace std;
int read(){
int a = 0; char c = getchar(); bool f = 0;
while(!isdigit(c)){f = c == '-'; c = getchar();}
while(isdigit(c)){
a = a * 10 + c - 48; c = getchar();
}
return f ? -a : a;
}
#define int long long
const int _ = 1e5 + 7; int A , B , C , N , M , T[_] , P[_];
int max(int a , int b){return a > b ? a : b;}
int calc(int x){
int sum = 0 , cntl = 0 , cntr = 0;
for(int i = 1 ; i <= N ; ++i) sum += max(x - T[i] , 0) * C;
for(int i = 1 ; i <= M ; ++i) P[i] <= x ? cntl += x - P[i] : cntr += P[i] - x;
return A > B ? sum + cntr * B : sum + min(cntl , cntr) * A + max(cntr - cntl , 0) * B;
}
signed main(){
cin >> A >> B >> C >> N >> M;
for(int i = 1 ; i <= N ; ++i) cin >> T[i];
for(int i = 1 ; i <= M ; ++i) cin >> P[i];
int L = 1 , R = 1e5;
if(C == 1e16) for(int i = 1 ; i <= M ; ++i) R = min(R , T[i]);
while(L < R){int mid = (L + R) >> 1; calc(mid) < calc(mid + 1) ? R = mid : L = mid + 1;}
cout << calc(L) << endl; return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int _ = 5e4 + 3; int N , P , M , C , T , arr[_]; vector < int > mu;
int pow1[61][_] , pow2[61][_]; bool flg1[61][_] , flg2[61][_];
int poww(int a , int b , int p){
int times = 1; bool flg = 0;
while(b){
if(b & 1){flg |= times * a >= p; times = times * a % p;}
flg |= a * a >= p; a = a * a % p; b >>= 1;
}
return times + (p != P && flg) * p;
}
namespace segt{
int cnt[_] , sum[_ << 2]; bool allv[_ << 2];
#define mid ((l + r) >> 1)
#define lch (x << 1)
#define rch (x << 1 | 1)
void init(int x , int l , int r){
if(l == r){cin >> arr[l]; sum[x] = arr[l]; return;}
else{init(lch , l , mid); init(rch , mid + 1 , r); sum[x] = sum[lch] + sum[rch];}
}
void modify(int x , int l , int r , int L , int R){
if(allv[x] || l > R || r < L) return;
if(l == r){
++cnt[l]; int val = poww(C , arr[l] , mu[cnt[l] - 1]);
for(int i = cnt[l] - 2 ; i >= 0 ; --i){
int p = val % T , q = val / T;
val = pow1[i][p] * pow2[i][q] % mu[i] + (bool)i * (flg1[i][p] || flg2[i][q] || pow1[i][p] * pow2[i][q] >= mu[i]) * mu[i];
}
sum[x] = val; allv[x] = mu[cnt[l] - 1] == 1; return;
}
modify(lch , l , mid , L , R); modify(rch , mid + 1 , r , L , R);
allv[x] = allv[lch] & allv[rch]; sum[x] = sum[lch] + sum[rch];
}
int qry(int x , int l , int r , int L , int R){
if(l >= L && r <= R) return sum[x];
int sum = 0;
if(mid >= L) sum = qry(lch , l , mid , L , R);
if(mid < R) sum += qry(rch , mid + 1 , r , L , R);
return sum;
}
}
int poww(int a , int b , int p , bool &flg){
int times = 1;
while(b){
if(b & 1){flg |= times * a >= p; times = times * a % p;}
flg |= a * a >= p; a = a * a % p; b >>= 1;
}
return times;
}
void initmu(){
mu.push_back(P); T = sqrt(2 * P) + 1;
while(*--mu.end() != 1){
int t = *--mu.end() , tmp = t , id = mu.size() - 1;
pow1[id][0] = pow2[id][0] = 1;
pow1[id][1] = poww(C , 1 , t , flg1[id][1]); pow2[id][1] = poww(C , T , t , flg2[id][1]);
for(int i = 2 ; i <= T ; ++i){
pow1[id][i] = pow1[id][i - 1] * pow1[id][1] % t;
pow2[id][i] = pow2[id][i - 1] * pow2[id][1] % t;
flg1[id][i] = flg1[id][i - 1] | (pow1[id][i - 1] * pow1[id][1] >= t);
flg2[id][i] = flg2[id][i - 1] | (pow2[id][i - 1] * pow2[id][1] >= t);
}
for(int i = 2 ; i * i <= tmp ; ++i)
if(tmp % i == 0){
t = t / i * (i - 1); while(tmp % i == 0) tmp /= i;
}
if(tmp - 1) t = t / tmp * (tmp - 1);
mu.push_back(t);
}
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
ios::sync_with_stdio(0); cin >> N >> M >> P >> C; segt::init(1 , 1 , N); initmu();
for(int i = 1 ; i <= M ; ++i){
int tp , l , r; cin >> tp >> l >> r;
if(tp) printf("%lld\n" , segt::qry(1 , 1 , N , l , r) % P);
else segt::modify(1 , 1 , N , l , r);
}
return 0;
}
#include<bits/stdc++.h>
//this code is written by Itst
using namespace std;
#define int long long
int N , P , R , K;
struct matrix{
int a[50][50];
matrix(){memset(a , 0 , sizeof(a));}
int* operator [](int x){return a[x];}
matrix operator *(matrix b){
matrix c;
for(int i = 0 ; i < K ; ++i)
for(int j = 0 ; j < K ; ++j)
for(int k = 0 ; k < K ; ++k)
c[i][k] = (c[i][k] + 1ll * a[i][j] * b[j][k]) % P;
return c;
}
}S , T;
signed main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
cin >> N >> P >> K >> R; N *= K;
S[0][0] = 1;
for(int i = 0 ; i < K ; ++i)
++T[i][i] , ++T[i][(i + 1) % K];
while(N){
if(N & 1) S = S * T;
T = T * T; N >>= 1;
}
cout << S[0][R];
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int read(){
int a = 0; char c = getchar(); bool f = 0;
while(!isdigit(c)){f = c == '-'; c = getchar();}
while(isdigit(c)){
a = a * 10 + c - 48; c = getchar();
}
return f ? -a : a;
}
const int _ = 1e5 + 7;
vector < int > ch[_];
int N , ans , fa[_]; int mxl[_] , mxn[_] , upl[_] , upn[_];
void dfs1(int x){
upl[x] = upn[x] = 0; int cnt = ch[x].size() - (x != 1); mxl[x] = 1; mxn[x] = 2;
for(auto t : ch[x])
if(t != fa[x]){
fa[t] = x; dfs1(t); mxn[x] = max(mxn[x] , max(mxn[t] - cnt + 2 , mxl[x] + mxl[t]));
mxl[x] = max(mxl[x] , mxl[t]);
}
mxl[x] += cnt - 1; mxn[x] += cnt - 2;
}
int p1[_] , p2[_] , s1[_] , s2[_];
void dfs2(int x){
ans = max(ans , upn[x] + mxn[x]);
int cnt = (int)ch[x].size() - 1;
p1[0] = s1[ch[x].size() + 1] = 1; p2[0] = s2[ch[x].size() + 1] = 2;
for(int i = 1 ; i <= ch[x].size() ; ++i){
int t = ch[x][i - 1] , p = t == fa[x] ? upl[x] : mxl[t] , q = t == fa[x] ? upn[x] : mxn[t];
p2[i] = max(p2[i - 1] , max(q - cnt + 2 , p1[i - 1] + p)); p1[i] = max(p1[i - 1] , p);
}
for(int i = ch[x].size() ; i ; --i){
int t = ch[x][i - 1] , p = t == fa[x] ? upl[x] : mxl[t] , q = t == fa[x] ? upn[x] : mxn[t];
s2[i] = max(s2[i + 1] , max(q - cnt + 2 , s1[i + 1] + p)); s1[i] = max(s1[i + 1] , p);
}
for(int i = 1 ; i <= ch[x].size() ; ++i)
if(ch[x][i - 1] != fa[x]){
int t = ch[x][i - 1]; upl[t] = max(p1[i - 1] , s1[i + 1]) + cnt - 1;
upn[t] = max(max(p2[i - 1] , s2[i + 1]) , p1[i - 1] + s1[i + 1]) + cnt - 2;
}
for(int i = 1 ; i <= ch[x].size() ; ++i) if(ch[x][i - 1] != fa[x]) dfs2(ch[x][i - 1]);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
for(int T = read() , x = read() ; T ; --T){
N = read(); for(int i = 1 ; i <= N ; ++i) ch[i].clear();
for(int i = 1 ; i <= 2 * x ; ++i) read();
for(int i = 1 ; i < N ; ++i){
int a = read() , b = read(); ch[a].push_back(b); ch[b].push_back(a);
}
ans = 0; dfs1(1); dfs2(1);
for(int i = 1 ; i <= N ; ++i)
if(ch[i].size() >= 2){
priority_queue < int > q;
for(auto t : ch[i]) q.push(t == fa[i] ? upn[i] : mxn[t]);
int sum = q.top(); q.pop(); sum += q.top(); q.pop();
ans = max(ans , sum + 1); while(!q.empty()) q.pop();
for(auto t : ch[i]) q.push(t == fa[i] ? upl[i] : mxl[t]);
for(int i = 1 ; i <= 4 ; ++i) q.push(1);
sum = 0; for(int i = 1 ; i <= 4 ; ++i){sum += q.top(); q.pop();}
ans = max(ans , sum + (int)ch[i].size() - 4);
}
printf("%d\n" , ans);
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int _ = 1e5 + 3;
int N , K , val , jc = 1 , arr[_];
int poww(long long a , int b){
int times = 1;
while(b){
if(b & 1) times = times * a % _;
a = a * a % _; b >>= 1;
}
return times;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
cin >> N >> K; for(int i = 1 ; i <= N ; jc = 1ll * jc * i++ % _) cin >> arr[i];
for(int i = N ; i ; --i)
if(arr[i]){
++val;
for(int j = 1 ; j * j <= i ; ++j)
if(i % j == 0){arr[j] ^= 1; if(i / j != j) arr[i / j] ^= 1;}
}
if(val <= K){printf("%lld\n" , 1ll * val * jc % _); return 0;}
int pre1 = 0 , pre2 = 1ll * N * poww(K + 1 , _ - 2) % _ , ans = pre2;
for(int i = K + 1 ; i < N ; ++i){
int p1 = 1ll * poww(N , _ - 2) * (N - i + 1) % _ , p3 = 1ll * poww(N , _ - 2) * (i + 1) % _;
int now = (pre2 - 1ll * pre1 * p1 % _ + _ - (i == val)) * poww(p3 , _ - 2) % _;
pre1 = pre2; pre2 = now; ans = (ans + now) % _;
}
cout << 1ll * (ans + K) * jc % _; return 0;
}
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = (a << 3) + (a << 1) + (c ^ '0');
c = getchar();
}
return f ? -a : a;
}
namespace flow{
const int MAXN = 1e5 + 7 , MAXM = 1e6 + 7;
struct Edge{
int end , upEd , f , c;
}Ed[MAXM];
int head[MAXN];
int N , M , S , T , cntEd = 1;
queue < int > q;
inline void addEd(int a , int b , int c , int d = 0){
Ed[++cntEd].end = b;
Ed[cntEd].upEd = head[a];
Ed[cntEd].f = c;
Ed[cntEd].c = d;
head[a] = cntEd;
}
inline void addE(int a , int b , int c , int d = 0 , bool f = 0){
addEd(a , b , c , d); addEd(b , a , c * f , -d);
}
int cur[MAXN] , dep[MAXN];
inline bool bfs(){
while(!q.empty())
q.pop();
q.push(S);
memset(dep , 0 , sizeof(dep));
dep[S] = 1;
while(!q.empty()){
int t = q.front();
q.pop();
for(int i = head[t] ; i ; i = Ed[i].upEd)
if(Ed[i].f && !dep[Ed[i].end]){
dep[Ed[i].end] = dep[t] + 1;
if(Ed[i].end == T){
memcpy(cur , head , sizeof(head));
return 1;
}
q.push(Ed[i].end);
}
}
return 0;
}
inline int dfs(int x , int mF){
if(x == T)
return mF;
int sum = 0;
for(int &i = cur[x] ; i ; i = Ed[i].upEd)
if(Ed[i].f && dep[Ed[i].end] == dep[x] + 1){
int t = dfs(Ed[i].end , min(mF - sum , Ed[i].f));
if(t){
Ed[i].f -= t;
Ed[i ^ 1].f += t;
sum += t;
if(sum == mF)
break;
}
}
return sum;
}
int Dinic(int s , int t){
int ans = 0;
S = s; T = t;
while(bfs())
ans += dfs(S , INF);
return ans;
}
}
int A[307] , lsh[307] , ind[307][307] , L , sum;
int main(){
#ifndef ONLINE_JUDGE
freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
int N = read() , M = read();
for(int i = 1 ; i <= N ; ++i)
lsh[i] = A[i] = read();
sort(lsh + 1 , lsh + N + 1);
L = unique(lsh + 1 , lsh + N + 1) - lsh - 1;
int S = 0 , T = N * (N + 1) / 2 + N + L + 1 , cnt = 0;
for(int i = 1 ; i <= N ; ++i)
for(int j = i ; j <= N ; ++j)
ind[i][j] = ++cnt;
for(int i = 1 ; i <= N ; ++i)
for(int j = i ; j <= N ; ++j){
int val = read();
if(val > 0){
sum += val;
flow::addE(S , ind[i][j] , val);
}
else flow::addE(ind[i][j] , T , -val);
if(i == j)
flow::addE(ind[i][j] , cnt + i , INF);
else{
flow::addE(ind[i][j] , ind[i + 1][j] , INF);
flow::addE(ind[i][j] , ind[i][j - 1] , INF);
}
}
for(int i = 1 ; i <= N ; ++i){
flow::addE(cnt + i , T , A[i]);
flow::addE(cnt + i , cnt + N + lower_bound(lsh + 1 , lsh + L + 1 , A[i]) - lsh , INF);
}
for(int i = 1 ; i <= L ; ++i)
flow::addE(cnt + N + i , T , M * lsh[i] * lsh[i]);
cout << sum - flow::Dinic(S , T);
return 0;
}
标签:nlog void 大于 子节点 dde bfs out 连通 memset
原文地址:https://www.cnblogs.com/Itst/p/11518660.html