$$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
",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)
现在考虑状态转移,(kk)表示上一轮与最终状态相反的个数,(x)表示有(x)个从和最终状态相反的变成和最终状态相同的,(y)表示从和最终状态相同的变成和最终状态不同的,则(f[i][j]=sum_{valid kk}f[i-1][kk]·C(kk,x)·C(n-kk,y))
其中需要满足四个约束条件:
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
",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)
最终状态是一棵树,只和边的数量有关,最多可以删掉(e-(v-1))条边
//#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)
权值线段树
假设现在桌上书的上限为(k),现在要读书的编号为(ID),那么如果上一次读编号为(ID)的书的时间到现在为止中间的读的其他的书总共不超过(k)本,那读这本编号为(ID)的书的时候就可以不用去书架拿了。所以只要记录当前这本书到之前读这本书之间读了多少种其他书即可,只要上限小于这个种类数的话就要重新从书架上拿,用权值线段树计算即可,最后用前缀和记录小于(i)的总数即可
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)
贪心,先变成(n)的倍数然后把低于平均数的补齐即可
//#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;
}
(G.Heap)
(H.Tokens on the Segments)
优先考虑左端点靠前的,每当遍历到某(x)值时,把左端点为(x)的线段的右端点加入到优先队列中,把已经超过右边界的线段删去,然后取右端点最靠左的放token
//#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;
}
(I.Connected Intervals)
(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{
int tot = 0,head[MAXN],nxt[MAXN<<3],to[MAXN<<3],cost[MAXN<<3];
bool vis[MAXN<<3];
void init(){
tot = 0;
memset(head,255,sizeof(head));
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);
G.ADDEDGE(mp[make_pair(i,j)],mp[make_pair(i+1,j)],c);
tot += c; nume++;
}
for(int i = 1; i < n; i++) for(int j = 1; j <= i; j++){
int c; scanf("%d",&c);
G.ADDEDGE(mp[make_pair(i,j)],mp[make_pair(i+1,j+1)],c);
tot += c; nume++;
}
for(int i = 1; i < n; i++) for(int j = 1; j <= i; j++){
int c; scanf("%d",&c);
G.ADDEDGE(mp[make_pair(i+1,j)],mp[make_pair(i+1,j+1)],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
%lld
",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)和(p),问满足(1le x le 2^p)且(a^x≡x^a(mod 2^p))的(x)有多少个
打表发现(a)是奇数的情况时答案必然是(1),接下来讨论(a)时偶数的情况
把(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))
也即:(k_0·x ge p Rightarrow a^x≡0 (mod 2^p))
然后考虑满足上述条件时(x^a)的情况,也就是要求(x^a≡0 (mod 2^p)),如果把(x)变成二进制形式(x=2^{s_0}+2^{s_1}+...+2^{s_p}),只要((2^{s_0})^a≡0 (mod 2^p)Rightarrow s_0·age p)就能满足要求,也就是对应的(x)二进制下的最低位要大于等于(frac{p}{a})(向上取整),找到最小的那个(x),其他的就是在范围内和(x)形成差为(2^{frac{p}{a}})的等差数列
最后把小的(x)特判一下
//#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
",res);
}
int main(){
for(scanf("%d",&T); T; T--) solve();
return 0;
}
(L.Median)
建大的向小的连边,同时建反图,如果图中存在环就直接全部输出0.
找每个点在拓扑排序序列中能够达到的最左端和最右端,如果((n+1)/2)在区间内的话,说明可以作为中位数,用bitset维护最少必经集合
//#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];
}
}
return tot;
}
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;
}