# The 10th Shandong Provincial Collegiate Programming Contest（11/13）

## $$The\ 10th\ Shandong\ Provincial\ Collegiate\ Programming\ Contest$$

### $A.Calandar$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
char day[5][20] = {{"Monday"},{"Tuesday"},{"Wednesday"},{"Thursday"},{"Friday"}};
using LL = int_fast64_t;
int T;
void solve(){
LL now = 0, y, m, d, nxt = 0;
int cd;
char s[20];
scanf("%I64d %I64d %I64d %s",&y,&m,&d,s);
if(s[0]=='M') cd = 0;
else if(s[0]=='T' && s[1]=='u') cd = 1;
else if(s[0]=='W') cd = 2;
else if(s[0]=='T' && s[1]=='h') cd = 3;
else cd = 4;
now = 360 * y + 30 * m + d;
scanf("%I64d %I64d %I64d",&y,&m,&d);
nxt = 360 * y + 30 * m + d;
LL delta = nxt - now;
cd = ((cd+delta)%5+5)%5;
printf("%s\n",day[cd]);
}
int main(){
for(scanf("%d",&T); T; T--) solve();
return 0;
}


### $B.Flipping\ Game$

DP
$f[i][j]$表示当前第$i$轮按开关，和最终状态相反的有$j$个的方案数，初始有$num$个灯和最终状态不同的话，显然$f[0][num]=1$

1.$0 \le x \le kk$
2.$0 \le y \le n-kk$
3.$x + y = m$
4.$kk-x+y=j$
$3$$4$联立得到$x=(kk+m-j)/2$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 111;
using LL = int_fast64_t;
const LL MOD = 998244353;
int T,n,m,k;
char s1[MAXN],s2[MAXN];
LL fact[MAXN],invfact[MAXN],f[2][MAXN];
LL qpow(LL a, LL b){
LL res = 1;
while(b){
if(b&1) res = res * a % MOD;
b >>= 1;
a = a * a % MOD;
}
return res;
}
LL C(int A, int B){ return fact[A] * invfact[B] % MOD * invfact[A-B] % MOD; }
void solve(){
scanf("%d %d %d %s %s",&n,&k,&m,s1,s2);
int num = 0;
for(int i = 0; i < n; i++) num += (s1[i]!=s2[i]?1:0);
memset(f,0,sizeof(f));
int tag = 0;
f[tag][num] = 1;
for(int i = 1; i <= k; i++){
tag ^= 1;
memset(f[tag],0,sizeof(f[tag]));
for(int j = 0; j <= n; j++){
for(int kk = max(0,j-m); kk <= min(n,j+m); kk++){
if((kk+m-j)&1) continue;
int x = ((kk+m-j)>>1);
if(x<0||x>kk||m-x<0||m-x>n-kk) continue;
f[tag][j] = (f[tag][j] + f[tag^1][kk] * C(kk,x) % MOD * C(n-kk,m-x)) % MOD;
}
}
}
printf("%lld\n",f[tag][0]);
}
int main(){
fact[0] = 1;
for(int i = 1; i < MAXN; i++) fact[i] = fact[i-1] * i % MOD;
for(int i = 0; i < MAXN; i++) invfact[i] = qpow(fact[i],MOD-2);
for(scanf("%d",&T); T; T--) solve();
return 0;
}


### $C.Wandering\ Robot$

#include<bits/stdc++.h>
#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int N = 1e5+10;
char a[N];
int32_t main(){
IOS;
int _;cin>>_;
while(_--){
int n,k; cin>>n>>k;
cin>>a;
int ans=0,x=0,y=0;
for(int i=0;i<n;i++){
if(a[i]=='R')x++;
if(a[i]=='L')x--;
if(a[i]=='U')y++;
if(a[i]=='D')y--;
ans=max(ans, abs(x)+abs(y));
}
if(k==1){
cout<<ans<<endl;
continue;
}
int xx=x*(k-1), yy=y*(k-1);
for(int i=0;i<n;i++){
if(a[i]=='R')xx++;
if(a[i]=='L')xx--;
if(a[i]=='U')yy++;
if(a[i]=='D')yy--;
ans=max(ans, abs(xx)+abs(yy));
}
cout<<ans<<endl;
}
return 0;
}


### $D.Game\ on\ a\ Graph$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int T,n,v,e;
char s[MAXN];
void solve(){
scanf("%d %s %d %d",&n,s,&v,&e);
for(int i = 1; i <= e; i++) scanf("%d %d",&n,&n);
n = strlen(s);
putchar(s[(e-(v-1))%n]=='1'?'2':'1'); puts("");
}
int main(){
for(scanf("%d",&T); T; T--) solve();
return 0;
}


### $E.BaoBao\ Loves\ Reading$

PS：当时写的时候用的是主席树，其实只要权值线段树就好了

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 2e5+7;
const int INF = 0x3f3f3f3f;
int T,n,A[MAXN],last[MAXN],gap[MAXN];
struct PersistentSegmentTree{
int tot,root[MAXN],sum[MAXN<<5],ls[MAXN<<5],rs[MAXN<<5];
void init(){ tot = 0; }
void update(int &now, int pre, int L, int R, int pos, int tag){
now = ++tot;
sum[now] = sum[pre] + tag;
ls[now] = ls[pre], rs[now] = rs[pre];
if(L+1==R) return;
int mid = (L+R) >> 1;
if(pos<mid) update(ls[now],ls[pre],L,mid,pos,tag);
else update(rs[now],rs[pre],mid,R,pos,tag);
}
int query(int L, int R, int pos, int rt){
if(L>=pos) return sum[rt];
if(R<=pos) return 0;
int mid = (L+R) >> 1;
return query(L,mid,pos,ls[rt]) + query(mid,R,pos,rs[rt]);
}
}PST;
void solve(){
memset(last,0,sizeof(last));
memset(gap,0,sizeof(gap));
PST.init();
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%d",&A[i]);
if(last[A[i]]){
int tp;
PST.update(tp,PST.root[i-1],1,n+1,last[A[i]],-1);
PST.update(PST.root[i],tp,1,n+1,i,1);
gap[PST.query(1,n+1,last[A[i]],PST.root[i-1])]++;
}
else PST.update(PST.root[i],PST.root[i-1],1,n+1,i,1);
last[A[i]] = i;
}
for(int i = 1; i <= n; i++) gap[i] += gap[i-1];
for(int i = 1; i <= n; i++) printf(i==n?"%d":"%d ",n-gap[i]);
puts("");
}
int main(){
for(scanf("%d",&T); T; T--) solve();
return 0;
}


### $F.Stones\ in\ the\ Bucket$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
using LL = int_fast64_t;
const int MAXN = 1e5+7;
int T,n;
LL A[MAXN];
void solve(){
LL res = 0;
cin >> n;
for(int i = 1; i <= n; i++) cin >> A[i];
LL tot = accumulate(A+1,A+1+n,0LL);
LL del = tot % n, ave = tot / n;
res = del;
for(int i = 1; i <= n; i++){
if(A[i]>ave&&del){
LL d = min(del,A[i]-ave);
del -= d;
A[i] -= d;
}
if(!del) break;
}
for(int i = 1; i <= n; i++) if(A[i]<ave) res += ave - A[i];
cout << res << endl;
}
int main(){
____();
for(cin >> T; T; T--) solve();
return 0;
}


### $H.Tokens\ on\ the\ Segments$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int T,n;
pair<int,int> seg[MAXN];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> seg[i].first >> seg[i].second;
sort(seg+1,seg+1+n);
int tot = 0, now = 1;
priority_queue<int,vector<int>,greater<int>> que;
for(int cur = 1; now<=n||!que.empty(); ){
while(now<=n&&seg[now].first==cur){
que.push(seg[now].second);
now++;
}
while(!que.empty()&&que.top()<cur) que.pop();
if(!que.empty()){
tot++;
que.pop();
}
if(!que.empty()) cur++;
else{
if(now>n) break;
else cur = seg[now].first;
}
}
cout << tot << endl;
}
int main(){
____();
for(cin >> T; T; T--) solve();
return 0;
}


### $J.Triangle\ City$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
using LL = int_fast64_t;
const int MAXN = 333*333;
const LL INF = 0x3f3f3f3f3f3f3f3f;
map<pair<int,int>,int> mp;
pair<int,int> rmp[MAXN];
struct Graph{
bool vis[MAXN<<3];
void init(){
tot = 0;
memset(vis,0,sizeof(vis));
}
void ADDEDGE(int u, int v, int c){
nxt[tot] = head[u]; to[tot] = v;
cost[tot] = c; head[u] = tot++;
nxt[tot] = head[v]; to[tot] = u;
cost[tot] = c; head[v] = tot++;
}
}G;
int T,n,pre[MAXN],iter[MAXN];;
LL dist[MAXN];
stack<int> stk;
LL Dijkstra(){
int s = 1, t = mp[make_pair(n,n)];
pre[s] = 0;
for(int i = 2; i <= (n+1)*n/2; i++) dist[i] = INF;
dist[s] = 0;
priority_queue<pair<LL,int>,vector<pair<LL,int>>,greater<pair<LL,int>>> que;
que.push(make_pair(dist[s],s));
while(!que.empty()){
auto p = que.top();
que.pop();
if(p.first!=dist[p.second]) continue;
for(int i = G.head[p.second]; ~i; i = G.nxt[i]){
int v = G.to[i];
if(dist[v]>dist[p.second]+G.cost[i]){
pre[v] = p.second;
dist[v] = dist[p.second] + G.cost[i];
que.push(make_pair(dist[v],v));
}
}
}
return dist[t];
}
void euler(int u){
for(int &i = iter[u]; ~i; i = G.nxt[i]){
if(G.vis[i]) continue;
G.vis[i] = G.vis[i^1] = true;
euler(G.to[i]);
}
stk.push(u);
}
void solve(){
scanf("%d",&n);
G.init();
LL tot = 0, nume = 0;
for(int i = 1; i < n; i++) for(int j = 1; j <= i; j++){
int c; scanf("%d",&c);
tot += c; nume++;
}
for(int i = 1; i < n; i++) for(int j = 1; j <= i; j++){
int c; scanf("%d",&c);
tot += c; nume++;
}
for(int i = 1; i < n; i++) for(int j = 1; j <= i; j++){
int c; scanf("%d",&c);
tot += c; nume++;
}
for(int i = 1; i <= (n+1)*n/2; i++) iter[i] = G.head[i];
tot -= Dijkstra();
int cur = mp[make_pair(n,n)];
while(pre[cur]){
nume--;
for(int i = G.head[pre[cur]]; ~i; i = G.nxt[i]){
int v = G.to[i];
if(v==cur){
G.vis[i] = G.vis[i^1] = true;
break;
}
}
cur = pre[cur];
}
nume++;
printf("%lld\n%lld\n",tot,nume);
euler(1);
while(!stk.empty()){
printf("%d %d ",rmp[stk.top()].first,rmp[stk.top()].second);
stk.pop();
}
puts("");
}
int main(){
int tag = 0;
for(int i = 1; i <= 333; i++) for(int j = 1; j <= i; j++){
mp[make_pair(i,j)] = ++tag;
rmp[tag] = make_pair(i,j);
}
for(scanf("%d",&T); T; T--) solve();
return 0;
}


### $K.Happy\ Equation$

$a$拆分为$2^{k_0}+2^{k_1}+2^{k_2}+...+2^{k_p}\ \ (k_0 < k_1 < k_2 < ... < k_p)$，则$a^x=(2^{k_0}+2^{k_1}+2^{k_2}+...+2^{k_p})^x$，只要$(2^{k_0})^x≡0\ (mod\ 2^p)$$a^x≡0\ (mod\ 2^p)$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
#define lowbit(x) ((x)&(-(x)))
using LL = int_fast64_t;
int T,a,p;
LL qpow(LL x, LL y, LL mod){
int res = 1;
while(y){
if(y&1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
void solve(){
scanf("%d %d",&a,&p);
if(a&1){
puts("1");
return;
}
int k0 = (int)log2(lowbit(a));
int minx = p/k0+(p%k0!=0?1:0);
int res = 0;
for(int x = 1; x < minx; x++) res += (qpow(a,x,1<<p)==qpow(x,a,1<<p)?1:0);
int s0 = p/a+(p%a!=0?1:0);
while(lowbit(minx)<(1<<s0)) minx += lowbit(minx);
res += ((1<<p)-minx+1)/(1<<s0)+1;
printf("%d\n",res);
}
int main(){
for(scanf("%d",&T); T; T--) solve();
return 0;
}


### $L.Median$

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 111;
int T,n,m,deg[MAXN],rdeg[MAXN];
vector<int> G[MAXN],rG[MAXN];
bitset<MAXN> bst[MAXN],rbst[MAXN];
int toposort(){
int tot = 0;
queue<int> que;
for(int i = 1; i <= n; i++) if(!deg[i]) que.push(i);
while(!que.empty()){
int u = que.front();
que.pop();
tot++;
for(int v : G[u]){
if(--deg[v]==0) que.push(v);
bst[v] |= bst[u];
}
}
}
void rtoposort(){
queue<int> que;
for(int i = 1; i <= n; i++) if(!rdeg[i]) que.push(i);
while(!que.empty()){
int u = que.front();
que.pop();
for(int v : rG[u]){
if(--rdeg[v]==0) que.push(v);
rbst[v] |= rbst[u];
}
}
}
void init(){
for(int i = 1; i <= n; i++){
G[i].clear();
rG[i].clear();
rdeg[i] = 0;
deg[i] = 0;
bst[i].reset();
bst[i].set(i);
rbst[i].reset();
rbst[i].set(i);
}
}
void solve(){
scanf("%d %d",&n,&m);
init();
for(int i = 1; i <= m; i++){
int u, v;
scanf("%d %d",&u,&v);
G[u].emplace_back(v);
rG[v].emplace_back(u);
deg[v]++;
rdeg[u]++;
}
int tot = toposort();
if(tot!=n){
for(int i = 0; i < n; i++) putchar('0');
puts("");
return;
}
rtoposort();
for(int i = 1; i <= n; i++){
int l = bst[i].count();
int r = n-rbst[i].count()+1;
if(l<=(n+1)/2 && (n+1)/2<=r) putchar('1');
else putchar('0');
}
puts("");
}
int main(){
for(scanf("%d",&T); T; T--) solve();
return 0;
}


### $M. Sekiro$

#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int N = 1e5+10;
int main(){
IOS;
int t; cin>>t;
while(t--){
int n,k; cin>>n>>k;
if(!n){
cout<<0<<endl;
continue;
}
while(k--){
if(n&1)n++;
n>>=1;
if(n==1)break;
}
cout<<n<<endl;
}
return 0;
}

posted @ 2020-02-07 20:09  _kiko  阅读(...)  评论(...编辑  收藏