ZOJ2019年1月月賽

BPMThor(BPM136)發表於2019-01-19

A

找規律
就是二進位制展開權重

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

ll KSM(ll a, ll k) {
	ll ret = 1;
	for (; k ; k >>= 1) {
		if (k & 1) 
			ret = ret * a;
		a = a * a;
	}
	return ret;
}

ll calc(ll n) {
	if (n == 0) 
		return 0;
	return calc(n - KSM(2, log2(n))) + 1;
}

int main() {
	// static int a[200][200];
	// a[1][1] = 1;
	// for (int i = 2; i <= 100; ++i)
	//     for (int j = 1; j <= i; ++j)
	//         a[i][j] = a[i - 1][j] + a[i - 1][j - 1],
	//         a[i][j] %= 2;
	// for (int i = 1; i <= 100; ++i) {
	//     // bitset<1000> v(i);
	//     // cout << v.count() << " : ";
	//     int sum = 0;
	//     for (int j = 1; j <= i; ++j)
	//         sum += a[i][j];
	//     cout << KSM(2, calc(i - 1)) << ' ';
	//     cout << sum << '\n';
	// }
	ios::sync_with_stdio(0);
	int T;
	cin >> T;
	while (T--) {
		ll n;
		cin >> n;
		cout << KSM(2, calc(n - 1)) << '\n';
	}
	return 0;
}

B

最開始沒有發現u,v也是1到N,於是大力上了線段樹。然而T的無法自理,ZOJ還討厭我的讀入掛,加了就不給我提交。
但是如果發現了u,v也是1到N的,那麼注意到答案點的y座標隨著x的增加是單調的,所以直接掃一遍就完事了

AC程式碼

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

using ll = long long;

inline ll read() {
	ll d = 0, f = 1; 
	char s = getchar();
	while (s < '0' || s > '9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9') {
		d = d * 10 + s - '0';
		s = getchar();
	}
	return d * f;
}

int const N = 1e5 + 5;

struct edge {
	int y, next;
}a[N], totx[N];
int lasta[N], ne, lastx[N], xne;
int n, m;

void add(int x, int y) {
	a[++ne].y = y;
	a[ne].next = lasta[x]; 
	lasta[x] = ne;
}

void addtot(int y, int x) {
	totx[++xne].y = x;
	totx[xne].next = lastx[y];
	lastx[y] = xne;
}

int solve(ll s) {
	for (int i = 1; i <= n; ++i)
		lastx[i] = 0;
	xne = 0;
	int ret = 0, ansy = n;
	ll sumx = 0, sumy = 0, sump = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = lasta[i]; j != 0; j = a[j].next) {
			int y = a[j].y;
			if (y <= ansy) {
				sumx += i;
				sumy += y;
				++sump;
				addtot(y, i);
			}
		}

		while (sump * (i + ansy) - sumx - sumy > s) {
			for (int j = lastx[ansy]; j != 0; j = totx[j].next) {
				int x = totx[j].y;
				sumx -= x;
				--sump;
				sumy -= ansy;
			}
			--ansy;
		}

		if (sump * (i + ansy) - sumx - sumy == s)
			++ret;
	}
	return ret;
}

int main() {
	int T = read();
	while (T--) {
		n = read(), m = read();
		for (int i = 1; i <= n; ++i)
			lasta[i] = 0;
		ne = 0;

		for (int i = 1; i <= m; ++i) {
			int x = read(), y = read();
			add(x, y);
		}

		int Q = read();
		while (Q--) {
			ll x = read();
			printf("%d", solve(x));
			if (Q == 0) 
				putchar('\n');
			else 
				putchar(' ');
		}
	}
}

如果和我最開始一樣傻逼沒注意到u和v的範圍怎麼辦呢?那麼雖然還是單調的,可是初始的y座標可以非常巨大。
那麼我們分成三部分統計

  • 列舉x,線段樹上二分確定y的位置來計算
  • 統計答案點如果大於所有的x和所有的y的情況的點數,注意到如果在n+1這個位置的答案的y座標不是一個整數,那麼在整個需要統計的區域都不會是整數(因為x每+1,y就需要-1,所以小數部分永遠是相同的)
  • 最後列舉y,統計有多少個對應的答案點的x是大於n的、

這樣時間複雜度就是O(nqlogn)了
程式碼如下(因為不加讀入掛就是T,加了應該還是,常數有點大,所以不能知道正確性)


/* ***********************************************
Author        :BPM136
Created Time  :1/19/2019 1:56:52 PM
File Name     :B.cpp
************************************************ */

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>

using namespace std;

using ll = long long;

inline ll read() {
	ll d = 0, f = 1; 
	char s = getchar();
	while (s < '0' || s > '9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9') {
		d = d * 10 + s - '0';
		s = getchar();
	}
	return d * f;
}

int const N = 1e5 + 5;

ll sum[N << 2], sumx[N << 2];
int pn[N << 2];
int p[N][2];
int n, m;

struct edge {
	int y, next;
}a[N];
int lasta[N], ne;

void pushup(int k) {
	pn[k] = pn[k << 1] + pn[k << 1 | 1];
	sum[k] = sum[k << 1] + sum[k << 1 | 1];
	sumx[k] = sumx[k << 1] + sumx[k << 1 | 1];
}

void SegAdd(int k, int l, int r, int x, int val) {
	if (l == r) {
		sum[k] += x;
		sumx[k] += val;
		++pn[k];
		return;
	}
	int mid = (l + r) / 2;
	if (x <= mid) 
		SegAdd(k << 1, l, mid, x, val);
	else
		SegAdd(k << 1 | 1, mid + 1, r, x, val);
	pushup(k);
}

ll sum_n, sum_x, sum_y;
ll SegEFin(int k, int l, int r, ll x, ll val) {
	if (l == r) {
		sum_n += pn[k];
		sum_y += sum[k];
		sum_x += sumx[k];
		if ((l + x) * sum_n - sum_x - sum_y == val)
			return l;
		else
			return 0;
	}
	int mid = (l + r) / 2;
	if ((mid + x) * (sum_n + pn[k << 1]) - (sum_x + sumx[k << 1]) - (sum_y + sum[k << 1]) >= val) 
		return SegEFin(k << 1, l, mid, x, val);
	else {
		sum_n += pn[k << 1];
		sum_x += sumx[k << 1];
		sum_y += sum[k << 1];
		return SegEFin(k << 1 | 1, mid + 1, r, x, val);
	}
}

ll SegEF(int x, ll val) {
	if (val < 0)
		return 0;
	sum_n = 0;
	sum_x = 0;
	sum_y = 0;
	return SegEFin(1, 1, n, x, val);
}

ll SegSum(int k, int l, int r, int _l, int _r) {
	if (l == _l && r == _r) 
		return sum[k];
	int mid = (l + r) / 2;
	if (_r <= mid) 
		return SegSum(k << 1, l, mid, _l, _r);
	if (_l > mid)
		return SegSum(k << 1 | 1, mid + 1, r, _l, _r);
	return SegSum(k << 1, l, mid, _l, mid) + SegSum(k << 1 | 1, mid + 1, r, mid + 1, _r);
}

ll q[20], Q, ans[20];

void solve() {
	for (int i = 0; i <= n * 4; ++i)
		pn[i] = sum[i] = sumx[i] = 0;
	memset(ans, 0, sizeof(ans));
	for (int i = 1; i <= n; ++i) {
		for (int j = lasta[i]; j != 0; j = a[j].next) {
			int x = a[j].y;
			SegAdd(1, 1, n, x, i);
		}
		for (int j = 1; j <= Q; ++j)
			ans[j] += SegEF(i, q[j]) > 0 ? 1 : 0;
	}
	ll _sumx = 0;
	for (int i = 1; i <= m; ++i) 
		_sumx += n + 1 - p[i][0];
	for (int i = 1; i <= Q; ++i) {
		ll posy = SegEF(n + 1, q[i] - _sumx);
		if (posy > n) 
			ans[i] += posy - n + 1;
	}
	for (int i = n - 1; i >= 1; --i) {
		for (int j = 1; j <= Q; ++j) {
			ll tmp = q[j] - (SegSum(1, 1, n, 1, i) + _sumx);
			ll td = tmp / n;
			if (tmp >= 0 && td * n == tmp)
				++ans[j];
		}
	}
}

void add(int x, int y) {
	a[++ne].y = y;
	a[ne].next = lasta[x]; 
	lasta[x] = ne;
}

int main() {
	ios::sync_with_stdio(0);
	int T = read();
	while (T--) {
		n = read(), m = read();
		for (int i = 0; i <= n; ++i)
			lasta[i] = 0;
		ne = 0;

		for (int i = 1; i <= m; ++i) {
			int x = read(), y = read();
			p[i][0] = x, p[i][1] = y;
			add(x, y);
		}

		Q = read();
		for (int i = 1; i <= Q; ++i)
			q[i] = read();
		solve();
		printf("%lld", ans[1]);
		for (int i = 2; i <= Q; ++i)
			printf(" %lld", ans[i]);
		putchar('\n');
	}
	return 0;
}

最後,那我們怎麼去掉這個log呢?
注意到我們剛剛統計x和y大於所有點的時候,如果n+1不是整數那麼全部都不是整數
同理,假如我們對應了一個x,那麼到下一個x的時候,假如不是整數,那麼同樣可以直接扔掉
否則下降到下一個x或者y的這一段,都將是答案
當x超出了之後,再次列舉y,同理維護即可
這樣時間複雜度就是O(nq)了

E

XD
我們列舉每一位,然後統計每一位的貢獻就好了

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

ll max(ll a, ll b) {
	if (a > b) 
		return a;
	else 
		return b;
}

int main() {
	ios::sync_with_stdio(0);
	// cerr << calc(1000000088888880ll, 10000001ll) << '\n';
	// return 0;
	int T;
	cin >> T;
	while (T--) {
		int k, m;
		cin >> k >> m;
		ll ans = 0, i;
		for (i = 1; i <= k; i *= 10) 
			ans += k / i - i + 1;
		--ans;
		if (ans >= m || (k - (i / 10) == 0 && ans < m - 1))
			cout << 0 << '\n';
		else {
			ll n = k;
			for (i = k - i / 10; ans < m - 1; n *= 10) {
				i *= 10;
				ans += i;
			}
			ll anss = n - ans + m - 2;
			cout << max(anss, k) << '\n';
		}
	}
	return 0;
}

I

相關文章