Add some material from day 1

parent 42d059a9
#include <cstdio>
#include <map>
#include <tuple>
#include <vector>
using namespace std;
#define MOD 1000000007
int N, M;
vector<char> s;
vector<int> g;
// DP[i, j, r]: in how many different ways we can fill in the '.' in
// s[i:] so that it matches with g[j:], if we must still
// get r '1' from the previous group. With r = 0 we must
// end the group, with r < 0 we have no obligation.
map<tuple<int, int, int>, int> DP;
int dp(int i, int j, int r) {
tuple<int, int, int> t(i, j, r);
auto it = DP.find(t);
if (it != DP.end()) return it->second;
if (i == N) return DP[t] = (j == M && r <= 0);
if (r > 0) return DP[t] = (s[i] == '0') ? 0 : dp(i+1, j, r-1);
switch (s[i]) {
case '0':
DP[t] = dp(i+1, j, -1);
break;
case '1':
DP[t] = (j < M && r < 0) ? dp(i, j+1, g[j]) : 0;
break;
case '.':
DP[t] = dp(i+1, j, -1);
if (j < M && r < 0) DP[t] = (DP[t] + dp(i, j+1, g[j])) % MOD;
break;
}
return DP[t];
}
int solve() {
DP.clear();
return dp(0, 0, -1);
}
int main() {
#ifdef CONTEST
freopen("bitsign.in", "rt", stdin);
freopen("bitsign.out", "wt", stdout);
#endif
int T;
scanf("%d", &T);
for (int t = 0; t < T; ++t) {
scanf("%d%d\n", &N, &M);
s.resize(N);
g.resize(M);
for (int i = 0; i < N; ++i) s[i] = getchar();
for (int i = 0; i < M; ++i) scanf("%d", &g[i]);
int result = solve();
printf("%d\n", result);
}
}
#!/bin/bash
attempt=1
while true; do
printf "\r$attempt"
./genlucky.py $* > in
./bf < in > out.bf
./opt < in > out.opt
diff out.bf out.opt || break
attempt=$((attempt+1))
done
#!/usr/bin/env python3
import random
import sys
N = int(sys.argv[1])
print(N)
L = [random.randrange(0, 1000) for _ in range(N)]
print(*L)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
#ifdef CONTEST
freopen("luckyagain.in", "rt", stdin);
freopen("luckyagain.out", "wt", stdout);
#endif
int N;
cin >> N;
vector<string> parts(N);
for (int i = 0; i < N; ++i) cin >> parts[i];
// O(N^2*D)
int count = 0;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j) {
if (i == j) continue;
string ticket = parts[i] + parts[j];
int s = ticket.size();
if (s % 2 != 0) continue;
int first = 0;
for (int k = 0; k < s/2; ++k) first += ticket[k] - '0';
int second = 0;
for (int k = s/2; k < s; ++k) second += ticket[k] - '0';
if (first == second) ++count;
}
cout << count << endl;
}
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
#define MAX_DIGITS 9
// O(N*D)
int main() {
#ifdef CONTEST
freopen("luckyagain.in", "rt", stdin);
freopen("luckyagain.out", "wt", stdout);
#endif
int N;
cin >> N;
vector<unordered_map<int, long long>> whole(1 + MAX_DIGITS);
vector<unordered_map<int, long long>> part(1 + MAX_DIGITS);
for (int i = 0; i < N; ++i) {
string piece;
cin >> piece;
int s = piece.size();
vector<int> prefix(s + 1);
prefix[0] = 0;
for (int k = 0; k < s; ++k) prefix[k + 1] = prefix[k] + piece[k] - '0';
int sum = prefix[s];
++whole[s][sum];
++part[s][sum];
for (int k = 1; 2 * k < s; ++k) {
++part[s - 2 * k][sum - 2 * prefix[k]]; // add to the right
++part[s - 2 * k][2 * prefix[s - k] - sum]; // add to the left
}
}
long long count = 0;
for (int length = 1; length <= MAX_DIGITS; ++length)
for (auto it : whole[length])
count += it.second * part[length][it.first];
cout << count - N << endl;
}
File added
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
#define MOD 1000000007
vector<int> price;
struct trie {
char letter;
int count;
vector<trie *> next;
trie(char l): letter(l), count(0), next(10, nullptr) {}
~trie() { for (auto p: next) delete p; }
void add_word(const char *x) {
++count;
if (x[0] != '\0') {
int i = x[0] - '0';
if (next[i] == nullptr) next[i] = new trie(x[0]);
next[i]->add_word(x+1);
}
}
pair<int, int> winnings(const char *y, int m=0) {
int s = 0, w = 0;
if (y[0] != '\0') {
int i = y[0] - '0';
if (next[i] != nullptr) {
auto p = next[i]->winnings(y+1, m+1);
s = p.first; w = p.second;
}
}
int n = m > 0 ? count : s;
int p = (w + (long long) (count - s) * price[m]) % MOD;
return make_pair(n, p);
}
};
int main () {
#ifdef CONTEST
freopen("lottery.in", "rt", stdin);
freopen("lottery.out", "wt", stdout);
#endif
int K, N, Q;
scanf("%d %d %d\n", &K, &N, &Q);
for (int i = 0, p = 1; i <= K; ++i, p = ((long long) 2 * p) % MOD)
price.push_back((p-1) % MOD);
trie t('$');
for (int i = 0; i < N; ++i) {
char x[K+1];
scanf("%s\n", x);
reverse(x, x+K);
t.add_word(x);
}
for (int i = 0; i < Q; ++i) {
char y[K+1];
scanf("%s\n", y);
reverse(y, y+K);
auto p = t.winnings(y);
printf("%d %d\n", p.first, p.second);
}
}
File added
File added
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment