题面
来源:USACO 官网。模型收到的是同一份题面的文字。
There are $N$ ($1\le N \leq 10^6$) cows in cow camp, labeled $1\dots N$. Each cow is either a camper or a coach.
A nonempty subset of the cows will be selected to attend a field trip. If the $i$th cow is selected, the cow will move to position $p_i$ ($0\le p_i \leq 10^9$) on a number line, where the array $p$ is strictly increasing.
A nonempty subset of the cows is called "good" if for every selected camper, there is a selected coach within $D$ units to the left, inclusive ($0\le D\le 10^9$). How many good subsets are there, modulo $10^9+7$?
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains two integers $N$ and $D$.The next $N$ lines each contain two integers $p_i$ and $o_i$. $p_i$ denotes the position the $i$th cow will move to. $o_i=1$ means the $i$th cow is a coach, whereas $o_i=0$ means the $i$th cow is a camper.
It is guaranteed that the $p_i$ are given in strictly increasing order.
OUTPUT FORMAT (print output to the terminal / stdout):
Output the number of good subsets modulo $10^9 + 7$.SAMPLE INPUT:
6 1 3 1 4 0 6 1 7 1 9 0 10 0
SAMPLE OUTPUT:
11
The last two campers can never be selected. All other nonempty subsets work as long as if cow $2$ is selected, then cow $1$ is also selected.
SAMPLE INPUT:
20 24 3 0 14 0 17 1 20 0 21 0 22 1 28 0 30 0 32 0 33 1 38 0 40 0 52 0 58 0 73 0 75 0 77 1 81 1 84 1 97 0
SAMPLE OUTPUT:
13094
SCORING:
- Input 3: $N=20$
- Input 4: $D=0$
- Inputs 5-8: $N\le 5000$
- Inputs 9-16: No additional constraints.
Problem credits: Agastya Goel, Eva Zhu, and Benjamin Qi
结果
Kimi K3.1 ProKimi
2/3
解出的采样 ↑
- 100采样 1 · 解出
- 93.8采样 2 · 答案错误
- 100采样 3 · 解出
- 4.6k平均输出 token(含思考) ↓
- 23.3输出速度 tokens/s ↑
- 195 秒延迟 ↓
- 0.92×CPU 时间 / 官方解 ↓
- 同阶 2时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 14.3k平均输出 token(含思考) ↓
- 191.1输出速度 tokens/s ↑
- 73 秒延迟 ↓
- 0.24×CPU 时间 / 官方解 ↓
- 更优 2、同阶 1时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
2/3
解出的采样 ↑
- 0.0采样 1 · 编译错误
- 100采样 2 · 解出
- 100采样 3 · 解出
- 18.8k平均输出 token(含思考) ↓
- 155.4输出速度 tokens/s ↑
- 116 秒延迟 ↓
- 0.29×CPU 时间 / 官方解 ↓
- 更优 2时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; static const long long INV2 = 500000004LL; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; long long D; cin >> N >> D; vector<int> qPos(N), qVal(N); int head = 0, tail = 0; long long total = 0; // Sum of all DP states. long long active = 0; // Sum of states whose last coach is still in the queue. long long mul = 1; // Common multiplier for queued values. long long invMul = 1; for (int i = 0; i < N; ++i) { long long p; int type; cin >> p >> type; if (type == 1) { // Select this coach after any previous state, or start with it alone. long long value = (total + 1) % MOD; total = (total + value) % MOD; active = (active + value) % MOD; qPos[tail] = static_cast<int>(p); qVal[tail] = static_cast<int>(value * invMul % MOD); ++tail; } else { long long leftLimit = p - D; // These coaches are too far left for this and all future campers. while (head < tail && static_cast<long long>(qPos[head]) < leftLimit) { long long value = static_cast<long long>(qVal[head]) * mul % MOD; active -= value; if (active < 0) active += MOD; ++head; } // Every active state may either include or exclude this camper. total = (total + active) % MOD; active = active * 2 % MOD; mul = mul * 2 % MOD; invMul = invMul * INV2 % MOD; } } cout << total << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; static const long long INV2 = 500000004LL; struct Coach { long long pos; long long base; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; long long D; cin >> N >> D; vector<Coach> coaches; coaches.reserve(N); size_t head = 0; // total includes the empty subset. long long total = 1; // Every active coach value is base * multiplier (mod MOD). long long multiplier = 1; long long invMultiplier = 1; long long activeBaseSum = 0; for (int i = 0; i < N; ++i) { long long p; int type; cin >> p >> type; if (type == 1) { // Selecting this coach can be appended to every existing subset. long long value = total; total = (total + value) % MOD; long long base = value * invMultiplier % MOD; activeBaseSum += base; if (activeBaseSum >= MOD) activeBaseSum -= MOD; coaches.push_back({p, base}); } else { long long leftmost = p - D; // These coaches are too far left for this and all future campers. while (head < coaches.size() && coaches[head].pos < leftmost) { activeBaseSum -= coaches[head].base; if (activeBaseSum < 0) activeBaseSum += MOD; ++head; } // Each subset whose latest selected coach is active may either // include or exclude this camper, so those states double. long long activeSum = multiplier * activeBaseSum % MOD; total += activeSum; if (total >= MOD) total -= MOD; multiplier = multiplier * 2 % MOD; invMultiplier = invMultiplier * INV2 % MOD; } } cout << total - 1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const long long MOD = 1000000007LL; static const long long INV2 = 500000004LL; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; long long D; cin >> N >> D; // Coaches that may still serve a future camper. vector<long long> coachPos(N); vector<long long> coachBase(N); int head = 0, tail = 0; long long total = 0; // Sum of all DP states. long long activeBase = 0; // Sum of base weights of active coaches. long long mult = 1; // Actual active weight = mult * base weight. long long invMult = 1; for (int i = 0; i < N; ++i) { long long p; int type; cin >> p >> type; if (type == 1) { // Appending this coach to any previous subset, or to the empty set. long long ways = (total + 1) % MOD; total += ways; if (total >= MOD) total -= MOD; long long base = ways * invMult % MOD; coachPos[tail] = p; coachBase[tail] = base; ++tail; activeBase += base; if (activeBase >= MOD) activeBase -= MOD; } else { long long leftLimit = p - D; while (head < tail && coachPos[head] < leftLimit) { activeBase -= coachBase[head]; if (activeBase < 0) activeBase += MOD; ++head; } // Every state whose latest coach is active gains one new subset // by selecting this camper, so all those state weights double. long long eligible = mult * activeBase % MOD; total += eligible; if (total >= MOD) total -= MOD; mult = mult * 2 % MOD; invMult = invMult * INV2 % MOD; } } cout << total << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; static const int INV2 = 500000004; static const int MAXN = 1000005; int coachPos[MAXN]; int coachRaw[MAXN]; struct FastScanner { static const int SZ = 1 << 20; int idx = 0, size = 0; char buf[SZ]; inline char readChar() { if (idx >= size) { size = (int)fread(buf, 1, SZ, stdin); idx = 0; if (size == 0) return 0; } return buf[idx++]; } template <class T> bool nextInt(T &out) { char c = readChar(); if (!c) return false; while (c != '-' && (c < '0' || c > '9')) { c = readChar(); if (!c) return false; } T sign = 1; if (c == '-') { sign = -1; c = readChar(); } T num = 0; while (c >= '0' && c <= '9') { num = num * 10 + (c - '0'); c = readChar(); } out = num * sign; return true; } }; int main() { FastScanner fs; int N, D; fs.nextInt(N); fs.nextInt(D); long long total = 1; long long G = 1; long long invG = 1; int m = 0; int head = 0; int sumRaw = 0; for (int i = 0; i < N; ++i) { int p, o; fs.nextInt(p); fs.nextInt(o); long long threshold = (long long)p - D; while (head < m && coachPos[head] < threshold) { sumRaw -= coachRaw[head]; if (sumRaw < 0) sumRaw += MOD; ++head; } if (o == 1) { int rawValue = (int)(total * invG % MOD); coachPos[m] = p; coachRaw[m] = rawValue; sumRaw += rawValue; if (sumRaw >= MOD) sumRaw -= MOD; ++m; total = total * 2 % MOD; } else { long long covered = (long long)sumRaw * G % MOD; total += covered; if (total >= MOD) total -= MOD; G = G * 2 % MOD; invG = invG * INV2 % MOD; } } long long answer = total - 1; if (answer < 0) answer += MOD; printf("%lld\n", answer); return 0; }
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; static const int INV2 = 500000004; struct FastScanner { static const int SZ = 1 << 20; int idx = 0, size = 0; char buf[SZ]; inline char getChar() { if (idx >= size) { size = (int)fread(buf, 1, SZ, stdin); idx = 0; if (size == 0) return 0; } return buf[idx++]; } bool nextInt(int &out) { char c = getChar(); if (!c) return false; while (c != '-' && (c < '0' || c > '9')) { c = getChar(); if (!c) return false; } int sign = 1; if (c == '-') { sign = -1; c = getChar(); } int num = 0; while (c >= '0' && c <= '9') { num = num * 10 + (c - '0'); c = getChar(); } out = num * sign; return true; } }; int main() { FastScanner fs; int N, D; if (!fs.nextInt(N)) return 0; fs.nextInt(D); vector<int> qpos(N), qbase(N); int head = 0, tail = 0; int total = 1; // includes the empty subset int activeSum = 0; // sum of C_j for active coaches int multiplier = 1; int invMultiplier = 1; for (int i = 0; i < N; i++) { int p, type; fs.nextInt(p); fs.nextInt(type); if (type == 1) { // Coach int v = total; int base = (long long)v * invMultiplier % MOD; qpos[tail] = p; qbase[tail] = base; tail++; activeSum += v; if (activeSum >= MOD) activeSum -= MOD; total += v; if (total >= MOD) total -= MOD; } else { // Camper long long threshold = (long long)p - D; while (head < tail && (long long)qpos[head] < threshold) { int actual = (long long)qbase[head] * multiplier % MOD; activeSum -= actual; if (activeSum < 0) activeSum += MOD; head++; } total += activeSum; if (total >= MOD) total -= MOD; activeSum += activeSum; if (activeSum >= MOD) activeSum -= MOD; multiplier = (long long)multiplier * 2 % MOD; invMultiplier = (long long)invMultiplier * INV2 % MOD; } } int answer = total - 1; if (answer < 0) answer += MOD; printf("%d\n", answer); return 0; }
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; static const int INV2 = 500000004; struct FastScanner { static const int BUFSIZE = 1 << 20; int idx = 0, size = 0; char buf[BUFSIZE]; inline char readChar() { if (idx >= size) { size = (int)fread(buf, 1, BUFSIZE, stdin); idx = 0; if (size == 0) return 0; } return buf[idx++]; } template <class T> bool nextInt(T &out) { char c; T sign = 1; T num = 0; c = readChar(); if (!c) return false; while (c != '-' && (c < '0' || c > '9')) { c = readChar(); if (!c) return false; } if (c == '-') { sign = -1; c = readChar(); } while (c >= '0' && c <= '9') { num = num * 10 + (c - '0'); c = readChar(); } out = num * sign; return true; } }; struct CoachState { int pos; int norm; }; int main() { FastScanner fs; int N, D; fs.nextInt(N); fs.nextInt(D); vector<CoachState> q; q.reserve(N); long long total = 1; // includes empty subset long long activeSum = 0; long long multiplier = 1; long long invMultiplier = 1; int head = 0; for (int i = 0; i < N; ++i) { int p, o; fs.nextInt(p); fs.nextInt(o); if (o == 0) { // Camper: can be selected only if the last selected coach is active. long long threshold = (long long)p - D; while (head < (int)q.size() && q[head].pos < threshold) { long long value = (long long)q[head].norm * multiplier % MOD; activeSum -= value; if (activeSum < 0) activeSum += MOD; ++head; } total += activeSum; if (total >= MOD) total -= MOD; activeSum += activeSum; if (activeSum >= MOD) activeSum -= MOD; multiplier = multiplier * 2 % MOD; invMultiplier = invMultiplier * INV2 % MOD; } else { // Coach: select it from any previous valid subset. long long oldTotal = total; long long normalized = oldTotal * invMultiplier % MOD; q.push_back({p, (int)normalized}); activeSum += oldTotal; if (activeSum >= MOD) activeSum -= MOD; total += oldTotal; if (total >= MOD) total -= MOD; } } long long answer = total - 1; if (answer < 0) answer += MOD; printf("%lld\n", answer); return 0; }
#include <bits/stdc++.h> using namespace std; static constexpr int MOD = 1'000'000'007; class FastScanner { static constexpr size_t BUFFER_SIZE = 1 << 16; char buffer[BUFFER_SIZE]; size_t index = 0, length = 0; int getChar() { if (index == length) { length = fread(buffer, 1, BUFFER_SIZE, stdin); index = 0; if (length == 0) return EOF; } return static_cast<unsigned char>(buffer[index++]); } public: bool readInt(int& value) { int c; do { c = getChar(); if (c == EOF) return false; } while (c <= ' '); value = 0; while (c >= '0' && c <= '9') { value = value * 10 + (c - '0'); c = getChar(); } return true; } }; struct CoachState { int position; int creationCount; int campersAtCreation; }; int main() { FastScanner input; int N, D; if (!input.readInt(N)) return 0; input.readInt(D); vector<int> powerOfTwo(N + 1); powerOfTwo[0] = 1; for (int i = 1; i <= N; ++i) { powerOfTwo[i] = (2LL * powerOfTwo[i - 1]) % MOD; } vector<CoachState> coachStates; coachStates.reserve(N); size_t head = 0; int nearCount = 0; int farCount = 1; // The empty subset. int campersProcessed = 0; for (int i = 0; i < N; ++i) { int p, type; input.readInt(p); input.readInt(type); // Expire coach states that cannot support a cow at position p. // Equality is allowed because the distance limit is inclusive. while (head < coachStates.size() && static_cast<long long>(coachStates[head].position) + D < p) { const CoachState& state = coachStates[head]; int currentCount = 1LL * state.creationCount * powerOfTwo[campersProcessed - state.camperAtCreation] % MOD; ++head; nearCount -= currentCount; if (nearCount < 0) nearCount += MOD; farCount += currentCount; if (farCount >= MOD) farCount -= MOD; } if (type == 1) { // Coach: every previous subset may omit or include this coach. int total = nearCount + farCount; if (total >= MOD) total -= MOD; nearCount += total; if (nearCount >= MOD) nearCount -= MOD; coachStates.push_back({p, total, campersProcessed}); } else { // Camper: only subsets with a sufficiently close coach may include it. nearCount = (2LL * nearCount) % MOD; ++campersProcessed; } } int answer = nearCount + farCount; if (answer >= MOD) answer -= MOD; // Exclude the empty subset. --answer; if (answer < 0) answer += MOD; printf("%d\n", answer); return 0; }
#include <bits/stdc++.h> using namespace std; static constexpr long long MOD = 1'000'000'007LL; static constexpr long long INV2 = 500'000'004LL; class FastScanner { static constexpr size_t BUFFER_SIZE = 1 << 16; char buffer[BUFFER_SIZE]; size_t index = 0, length = 0; int getChar() { if (index == length) { length = fread(buffer, 1, BUFFER_SIZE, stdin); index = 0; if (length == 0) return EOF; } return static_cast<unsigned char>(buffer[index++]); } public: template <typename T> bool readInt(T& result) { int c; do { c = getChar(); if (c == EOF) return false; } while (c <= ' '); bool negative = false; if (c == '-') { negative = true; c = getChar(); } T value = 0; while (c >= '0' && c <= '9') { value = value * 10 + (c - '0'); c = getChar(); } result = negative ? -value : value; return true; } }; struct CoachState { long long position; int storedCount; }; int main() { FastScanner scanner; int N; long long D; if (!scanner.readInt(N)) return 0; scanner.readInt(D); vector<CoachState> states; states.reserve(N); size_t head = 0; // Number of valid subsets of the processed prefix, including the empty set. long long total = 1; // Actual count of a state = storedCount * scale (mod MOD). long long scale = 1; long long inverseScale = 1; long long sumStored = 0; for (int i = 0; i < N; ++i) { long long p; int type; scanner.readInt(p); scanner.readInt(type); long long cutoff = p - D; // A coach exactly D units to the left is still usable. while (head < states.size() && states[head].position < cutoff) { sumStored -= states[head].storedCount; if (sumStored < 0) sumStored += MOD; ++head; } if (type == 1) { // Selecting this coach creates one new state for every old valid subset. int storedCount = static_cast<int>(total * inverseScale % MOD); states.push_back({p, storedCount}); sumStored += storedCount; if (sumStored >= MOD) sumStored -= MOD; // Either skip or select this coach. total = total * 2 % MOD; } else { // Only states whose latest selected coach is within D may select it. long long eligible = sumStored * scale % MOD; total += eligible; if (total >= MOD) total -= MOD; // Every eligible state's count doubles. scale = scale * 2 % MOD; inverseScale = inverseScale * INV2 % MOD; } } // The empty subset is valid but must not be counted. printf("%lld\n", (total + MOD - 1) % MOD); return 0; }
#include <bits/stdc++.h> using namespace std; static constexpr int MOD = 1'000'000'007; class FastScanner { static constexpr size_t BUFFER_SIZE = 1 << 16; char buffer[BUFFER_SIZE]; size_t index = 0, size = 0; int getChar() { if (index == size) { size = fread(buffer, 1, BUFFER_SIZE, stdin); index = 0; if (size == 0) return EOF; } return static_cast<unsigned char>(buffer[index++]); } public: bool readInt(int& value) { int c; do { c = getChar(); if (c == EOF) return false; } while (c <= ' '); value = 0; while (c >= '0' && c <= '9') { value = value * 10 + (c - '0'); c = getChar(); } return true; } }; inline int addMod(int a, int b) { int result = a + b; if (result >= MOD) result -= MOD; return result; } struct Coach { int position; int base; int campersBefore; }; int main() { FastScanner input; int N, D; if (!input.readInt(N)) return 0; input.readInt(D); vector<int> powerOfTwo(N + 1); powerOfTwo[0] = 1; for (int i = 1; i <= N; ++i) { powerOfTwo[i] = addMod(powerOfTwo[i - 1], powerOfTwo[i - 1]); } vector<Coach> coaches; coaches.reserve(N); size_t head = 0; int total = 1; // Includes the empty selection. int active = 0; // Sum of states whose latest coach is still usable. int campersSeen = 0; for (int i = 0; i < N; ++i) { int position, type; input.readInt(position); input.readInt(type); long long minimumPosition = static_cast<long long>(position) - D; // These coaches can no longer support this or any later cow. while (head < coaches.size() && static_cast<long long>(coaches[head].position) < minimumPosition) { const Coach& coach = coaches[head]; int exponent = campersSeen - coach.campersBefore; int currentWeight = static_cast<int>( 1LL * coach.base * powerOfTwo[exponent] % MOD ); active -= currentWeight; if (active < 0) active += MOD; ++head; } if (type == 1) { // Selecting this coach creates a new state from every valid prefix. int base = total; coaches.push_back({position, base, campersSeen}); active = addMod(active, base); total = addMod(total, total); } else { // Active states may either omit or select this camper. int oldActive = active; total = addMod(total, oldActive); active = addMod(oldActive, oldActive); ++campersSeen; } } int answer = total - 1; // Exclude the empty selection. if (answer < 0) answer += MOD; printf("%d\n", answer); return 0; }