牛客多校第一場補題
難度升序
F:Infinite String Comparision
上手就寫,將ab都延長2倍,然後比較到max(a.length,b.length)即可
證法:假設a.length<b.length,然後舉例子就行了
#pragma GCC optimize(2) #include<bits/stdc++.h> #define ll long long #define fastio {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}; using namespace std; double pi = acos(-1); const double eps = 1e-9; const int inf = 1e9 + 7; const int maxn = 2e3 + 10; int judge(string a,string b) { int lena = a.length(), lenb = b.length(); int len = max(lena, lenb); for (int i = 0; i < len; i++) { int l = i % lena, r = i % lenb; if (a[l] < b[r])return -1; else if (a[l] > b[r])return 1; } return 0; } int main() { fastio; string a, b; while (cin >> a >> b) { a += a, b += b; if (judge(a, b) == 1)cout << ">" << endl; else if(judge(a, b) == 0)cout << "=" << endl; else cout << "<" << endl; } return 0; }
J-Easy Integration
求這個積分,如果是分數就輸出分子乘分母的逆元再模998244353
手殘導致輸出溢位WA了,我只能爪巴
#pragma GCC optimize(2) #include<bits/stdc++.h> #define ll long long #define fastio {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}; using namespace std; double pi = acos(-1); const double eps = 1e-9; const int inf = 1e9 + 7; const int maxn = 1e6 + 10; const ll mod = 998244353; ll f[maxn], fac[maxn], tmp[maxn]; long long qpow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n /= 2; } return res%mod; } int main() { fastio; ll n; f[0] = 1; fac[0] = 1; tmp[1] = 2; for (ll i = 1; i < maxn; i++) fac[i] = (fac[i - 1] * (2 * i + 1)) % mod; for (ll i = 2; i < maxn; i++) tmp[i] = (tmp[i - 1] * 2 * i) % mod; while (cin >> n) { cout << (((qpow(qpow(4, n), mod - 2) % mod) * (tmp[n] % mod))%mod * (qpow(fac[n], mod - 2) % mod)) % mod << endl; } }
I-1 or 2
很明顯是個網路流,但需要一點建圖的技巧
構造源點流量為inf,連線輸入點1~n,流量為x;然後將1+N~n+N與輸入點按照題目要求相連,每條邊的流量為1;再將1+N~n+N進行拆點限流,最後彙總到匯點跑dinic檢查是否能跑到最大流即可
(程式碼有點不同,思路一樣,板子備註沒刪)
#pragma GCC optimize(2) #include<bits/stdc++.h> #define ll long long #define fastio {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}; using namespace std; double pi = acos(-1); const double eps = 1e-9; const int inf = 1e9 + 7; const int maxn = 1e6 + 10; int cnt = 1, head[maxn], now[maxn], deep[maxn]; queue<int>q; struct edge { int to; int cost; int next; }e[maxn * 2]; void add(int from, int to, int cost)//前向星 { e[++cnt].to = to; e[cnt].cost = cost; e[cnt].next = head[from]; head[from] = cnt; } int n, m, s, t;//n個點,m條邊,s為源點,t為匯點 bool bfs() { memset(deep, 0, sizeof(deep));//初始化深度 while (!q.empty())q.pop();//初始化佇列 q.push(s); deep[s] = 1;//源點深度賦值1 now[s] = head[s];//將擁有深度的點複製到另一個head陣列 while (!q.empty()) { int from = q.front(), to, cost; q.pop(); for (int i = head[from]; i; i = e[i].next) { to = e[i].to; cost = e[i].cost; if (cost && !deep[to])//容量>0且未遍歷過 { q.push(to); deep[to] = deep[from] + 1; now[to] = head[to];//將擁有深度的點複製 if (to == t)return 1;//找到匯點就return,在deep[t]-1的點早已被複制,所以不用擔心直接返回會丟失增廣路 } } } return 0; } int dfs(int from, int flow) { if (from == t)return flow; int rest = flow, tmp, i;//rest存當前剩餘容量,tmp存當前增廣路可增廣流量,i前向星 //cout << 1 << endl; for (i = now[from]; i && rest; i = e[i].next)//嘗試增廣與from相連的所有容量>0且deep[from]+1==deep[to]的點所存在的路徑 if (e[i].cost && deep[e[i].to] == deep[from] + 1) { tmp = dfs(e[i].to, min(flow, e[i].cost));//取當前路徑上最小容量作為flow if (!tmp)deep[e[i].to] = -2;//to所有邊都無法增廣,那就炸點,直接把深度賦一個不可能值 e[i].cost -= tmp;//增廣完成後正向邊減去tmp,反向邊加上tmp,剩餘容量減去tmp e[i ^ 1].cost += tmp;//由於存圖是從2開始存並且正邊和反邊相鄰,所以正邊^1是反邊,反之亦然 rest -= tmp; //cout << rest << endl; } now[from] = i;//當前弧優化,下次遍歷到from時會直接從i(flow完了i就不一定是0了)開始 return flow - rest;//返回一個增廣消耗的值 } ll dinic() { long long maxflow = 0; long long flow = 0; while (bfs()) { //for (int i = 1; i <= n; i++) // cout << deep[i] << " "; while (flow = dfs(s, inf))//dfs沒能找到增廣路就重新進行分層 maxflow += flow; } return maxflow; } int main() { fastio; int n, m; s = 200, t = 301; while (cin >> n >> m) { cnt = 1; memset(head, 0, sizeof(head)); int sum = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; sum += x; add(i, i + 100, 0); add(i + 100, i, x);//限流器 add(i, t, inf); add(t, i, 0); add(s, i + 200, x); add(i + 200, s, x); } for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; add(a + 200, b + 100, 1); add(b + 100, a + 200, 0); add(b + 200, a + 100, 1); add(a + 100, b + 200, 0); } if (dinic() == sum)cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
H-Minimum-cost Flow
最小費用流,題目直接告訴做法了,直接跑就完事了(我板子好像常數有點問題,cin直接原地爆炸)
還是有丶做法:首先跑流量無限,所有邊容量為1的最小費用流,記錄每一條增廣路的花費(每一條都是滿流);
然後對於每一對uv,讓每條邊的容量都變成u,那麼每一條增廣路的花費也會變成u倍;
由於源點是單位流,邊權原本是u/v,現在變成了u,那麼源點的流量就變成了v,因此最大流(或者說答案需求流)必然只能是v
設當前已增廣的流量為flownow
flownow=0
這樣遍歷每一條增廣路(已知必然滿流),若flownow+u<=v,則flownow += u, cost += tracost[i] * u;
如果flownow+u>v,但flownow<v,這一條就跑不滿,cost += tracost[i] * (v - tmp), flownow = v;
如果跑完之後flownow<v,則跑不滿單位流,輸出NaN;
如果跑滿了,就把cost和v求個gcd,然後輸出就完事了
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
double pi = acos(-1);
const double eps = 1e-9;
const int inf = 1e9 + 7;
const int maxn = 60;
int cnt = 1, head[maxn], pre[maxn], last[maxn];
int dis[maxn], flow[maxn], maxflow;
int vis[maxn];
vector<ll>tracost;
queue<int>q;
int n, m, S, T;
struct edge {
int to;
int cost;
int vue;
int next;
}e[maxn * 4];
inline void add(int from, int to, int cost, int vue)//前向星
{
e[++cnt].to = to;
e[cnt].cost = cost;
e[cnt].vue = vue;
e[cnt].next = head[from];
head[from] = cnt;
}
ll __gcd(ll a, ll b)
{
while (b)
{
ll tmp = b;
b = a % b;
a = tmp;
}
return a;
}
ll SPFA(int s, int t) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
q.push(s);
dis[s] = 0, vis[s] = 1, pre[t] = -1, flow[s] = inf;
while (!q.empty()) {
int now = q.front();
q.pop();
vis[now] = 0;
for (int i = head[now]; i != -1; i = e[i].next) {
if (e[i].cost > 0) {
int po = e[i].to;
long long lo = e[i].vue;
if (dis[po] > dis[now] + lo) {
dis[po] = dis[now] + lo;
flow[po] = min(flow[now], e[i].cost);
last[po] = i;
pre[po] = now;
if (!vis[po]) {
vis[po] = 1;
q.push(po);
}
}
}
}
}
return pre[t] != -1;
}
void MCMF(int s, int t) {
maxflow = 0;
while (SPFA(s, t)) {
maxflow += flow[t];
tracost.push_back(dis[t] * flow[t]);
for (int i = t; i != s; i = pre[i]) {
e[last[i]].cost -= flow[t],
e[last[i] ^ 1].cost += flow[t];
}
}
}
int main()
{
S = 1;
while (~scanf("%d %d", &n, &m))
{
memset(head, -1, sizeof(head));
tracost.clear();
cnt = 1;
T = n;
for (int i = 1; i <= m; ++i)
{
ll a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, 1, c);
add(b, a, 0, -c);
}
MCMF(S, T);
int q;
scanf("%d", &q);
int u, v;
while (q--)
{
scanf("%d%d", &u, &v);
ll tmp = 0, cost = 0;
int size = tracost.size();
for (int i = 0; i < size; i++)
{
if (tmp + u <= v)
tmp += u, cost += tracost[i] * u;
else if (tmp < v)
cost += tracost[i] * (v - tmp), tmp = v;
else
break;
}
if (tmp == v)
{
ll gcd = __gcd(cost, v);
printf("%lld/%lld\n", cost / gcd, v / gcd);
}
else printf("NaN\n");
}
}
return 0;
}