We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: 0x4F5F6F
Tutorial
D1 – Interdimensional Grammar (easy version)
Author: 0x4F5F6F
Let's define $$$A[n][n]$$$ and $$$dp[m][n]$$$ where $$$A[i][j]$$$ represents whether $$$j$$$th character can follow $$$i$$$th character or not where $$$A[i][j]$$$ being $$$1$$$ will mean $$$j$$$th character can follow $$$i$$$th character and $$$A[i][j]$$$ being $$$0$$$ will mean $$$j$$$th character cannot follow $$$i$$$th character and $$$dp[i][j]$$$ will represent number of words of length $$$i+1$$$ ending with $$$jth$$$ character.
$$$A[n][n]$$$ is already given in the input and we can initially set $$$dp[0][j]$$$ for $$$0 \le j \lt n$$$ to $$$1$$$ because word with length $$$1$$$ ending with $$$ith$$$ character will be $$$1$$$, further we can define $$$dp[i][j]$$$ for $$$1 \le i \lt m$$$ and $$$0\le j \lt n$$$ as follows:- \begin{align} dp[i][j]=\sum_{k=0}^{n-1}dp[i-1][k]⋅A[k][j] \end{align} Using this we can calculate $$$dp[i][j]$$$ in $$$\mathcal{O}(n)$$$ time and populate the whole $$$dp[m][n]$$$ in $$$\mathcal{O}(n^2m)$$$ time. Now we can sum up all the values in $$$dp[m][n]$$$ to get all the words of length from $$$1$$$ to $$$m$$$.
Time Complexity: $$$\mathcal{O}(n^2m)$$$
Space Complexity: $$$\mathcal{O}(nm)$$$
#include<bits/stdc++.h>
using namespace std;
int mod = 1000000007;
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d %d", &n, &m);
vector<vector<int>> A(n, vector<int>(n));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
vector<vector<long long>> dp(m, vector<long long>(n, 0));
for(int i = 0; i < n; i++) {
dp[0][i] = 1;
}
for(int i = 1; i < m; i++) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++) {
dp[i][j] = (dp[i][j] + (dp[i-1][k] * A[k][j]) % mod) % mod;
}
}
}
long long c{0};
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
c = (c + dp[i][j]) % mod;
}
}
printf("%lld\n", c);
}
return 0;
}
D2 – Interdimensional Grammar (hard version)
Author: 0x4F5F6F
Let's define a $$$n$$$x$$$n$$$ matrix $$$A$$$, where $$$A_{ij}$$$ represent if $j$th character can follow
Time Complexity: $$$\mathcal{O}(n^3\log_2m)$$$
Space Complexity: $$$\mathcal{O}(n^2)$$$
#include<bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
vector<vector<long long>> matrixProduct(vector<vector<long long>>& a, vector<vector<long long>>& b, int n) {
vector<vector<long long>> r(n, vector<long long>(n, 0));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++) {
r[i][j] = (r[i][j] + ((a[i][k] * b[k][j]) % mod)) % mod;
}
}
}
return r;
}
vector<vector<long long>> matrixPower(vector<vector<long long>> &a, int n, int p) {
vector<vector<long long>> k{a}, r(n, vector<long long>(n, 0));
for(int i = 0; i < n; i++) {
r[i][i] = 1;
}
while(p) {
if(p & 1) {
r = matrixProduct(r, k, n);
}
k = matrixProduct(k, k, n);
p = p >> 1;
}
return r;
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d %d", &n, &m);
vector<vector<long long>> A(n+1, vector<long long>(n+1, 0));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%lld", &A[i][j]);
}
}
for(int i = 0; i <= n; i++) {
A[i][n] = 1;
}
A = matrixPower(A, n + 1, m);
long long r{};
for(int i = 0; i < n; i++) {
r = (r + A[i][n]) % mod;
}
printf("%lld\n", r);
}
return 0;
}
Author: _bLIC_
Lets first sort all three arrays $$$a$$$, $$$b$$$, and $$$c$$$ in increasing order of array $$$a$$$.
Lets call subarray $$$a_{l, r}$$$, $$$l\leq r$$$, good if $$$a_r-a_l\leq x$$$. For a good subarray, we can choose any $$$i$$$ and any $$$j$$$ in the subarray $$$[l, r]$$$, maintaining the given condition $$$|a_i - a_j| \leq x$$$. It is always optimal to choose $$$i$$$ with the maximum $$$b$$$ and $$$j$$$ with the maximum $$$c$$$ from good subarray.
Now, for each $$$r = 1, 2, ,..., n$$$,
- we find the minimum left index $$$l$$$ for which $$$a_{l, r}$$$ is good. This can be done by two pointers or binary search.
- Update the answer with the sum of the maximum of $$$b$$$ and the maximum of $$$c$$$ in the subarray $$$[l, r]$$$.
The maximum in the subarray can be found using a segment tree. A multiset can also be used to keep track of elements in the subarrays of $$$b$$$ and $$$c$$$ separately.
The overall time complexity will be $$$O(n\log n)$$$.
#include <bits/stdc++.h>
using namespace std;
// Source: https://codeforces.me/blog/entry/18051
template <class T> struct SegTree{
int n; T ID;
std::vector<T> tree;
T oper(T a, T b){return max(a, b);} // update this operation function according to need
SegTree(int _n, T id){
n = 1;ID = id;
while(n<_n) n<<=1;
tree.resize(2*n, ID);
}
void add(int i, T val){tree[i+n] = val;}
void build(){
for (int i = n-1;i>0;--i) tree[i] = oper(tree[i<<1], tree[i<<1|1]);
}
void update(int ind, T value){
for (tree[ind+=n] = value;ind>1;ind>>=1) tree[ind>>1] = (ind&1)?oper(tree[ind^1], tree[ind]):oper(tree[ind], tree[ind^1]);
}
T query(int l, int r){// indexing is from 0 to n-1, gives answer to [l, r)
T left = ID, right = ID; // left and right for non associative operation
for (l+=n, r+=n;l<r;l>>=1, r>>=1){
if (l&1) left = oper(left, tree[l++]);
if (r&1) right = oper(tree[--r], right);
}
return oper(left, right);
}
};
void solve(){
int n, x;cin>>n>>x;
array<int, 3> abc[n];
for (int i = 0;i<n;i++) cin>>abc[i][0];
for (int i = 0;i<n;i++) cin>>abc[i][1];
for (int i = 0;i<n;i++) cin>>abc[i][2];
sort(abc, abc+n);
SegTree<int> c(n, 0), b(n, 0);
for (int i = 0; i<n;i++){
b.add(i, abc[i][1]);
c.add(i, abc[i][2]);
}
b.build();
c.build();
int ans = 0;
int l = 0;
for (int r = 0;r<n;r++){
while(abc[r][0]-abc[l][0]>x){
l++;
}
ans = max(ans, b.query(l, r+1) + c.query(l, r+1));
}
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;cin>>t;
while(t--){
solve();
cout<<'\n';
}
}
Author: _bLIC_
We are given function
Let define $$$p_v =$$$ parent of $$$v$$$.
Let $$$S_{u, v}$$$ be the set of vertices on the path from $$$u$$$ to $$$v$$$ ($$$S_{u, v}=S_{v, u}$$$). Note for each pair $$$(u, v)$$$ there is exactly one path in tree and hence there will be exactly one set of vertices. Lets define $$$g(u, v)$$$ as $$$g(u, v) = \prod_{x\in S_{u, v}}{a_x}$$$, which is just the product of values of vertices on the path from $$$u$$$ to $$$v$$$.
Now if you try to expand the $$$\text{RHS}$$$ of given function $$$f(v)$$$ then you may notice $$$f(v)$$$ is just sum of product of values of vertices on the path from $$$v$$$ to each vertex in the subtree of $$$v$$$ i.e.
multiplying both side by $$$g(1, p_v)$$$ gives,
both paths $$$(1 \text{ to } p_v)$$$ and $$$(v \text{ to }u)$$$ are disjoint and can also be merged to form new path $$$(1 \text{ to } u)$$$ giving $$$g(1, p_v) \cdot g(v, u) = g(1, u)$$$,
Now if we redefine $$$g(1, v)$$$ as new function $$$h(v)$$$ then the above equality becomes
Now we can use euler tour with lazy segment tree(which store $$$h(i)$$$ in the order of euler tour) to find $$$h(p_v)$$$ and the sum of $$$h(u)$$$ in the subtree of $$$v$$$. Also updating the value of node only change the value of the nodes in the subtree of that node which can be done as range update by also lazy segment tree. More formally to handle queries:
- $$$1$$$ $$$v$$$ $$$x$$$, this change in $$$a_v = x$$$ only effect the $$$h$$$ of vertices in the subtree of vertex $$$v$$$ and can be updated by multiplying $$$\frac{x}{a_v}$$$ in the lazy segment tree in the range representing the subtree of $$$v$$$
- $$$2$$$ $$$v$$$, this can be handled by simply sum querying the range representing the subtree of $$$v$$$ and dividing by $$$h(p_v)$$$ which can also be queried on segment tree
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;
constexpr int P = 1e9+7;
using i64 = long long;
// assume -P <= x < 2P
int normalize(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int _x = 0) : x(normalize(_x)) {}
Z(i64 _x) : x(normalize(_x % P)) {}
int val() const {
return x;
}
Z operator-() const {
return Z(normalize(P - x));
}
Z inv() const {
assert(x != 0);
return power(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = normalize(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = normalize(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, Z &a) {
i64 v;
is >> v;
a = Z(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Z &a) {
return os << a.val();
}
};
template <class T>
struct SegTree{
int n;
std::vector<T> tree, lz;
std::vector<bool> haslazy;
SegTree(int _n){
n = 1;
while(n<_n) n*=2;
tree.assign(2*n, T(1));
lz.assign(2*n, T(1));
haslazy.assign(2*n, false);
}
void pushup(int node){
tree[node] = tree[node<<1] + tree[node<<1|1];
}
void pushdown(int node){
if (!haslazy[node]) return;
tree[node<<1] *= lz[node];
tree[node<<1|1] *= lz[node];
lz[node<<1] *= lz[node];
lz[node<<1|1] *= lz[node];
haslazy[node<<1] = true;
haslazy[node<<1|1] = true;
lz[node] = T(1);
haslazy[node] = false;
}
void add(int i, T v){
tree[i+n] = v;
}
void build(){
for (int i = n-1;i>0;i--) pushup(i);
}
void update(int l, int r, T val, int x, int lx, int rx){
if (l<=lx && rx<=r) {
tree[x] *= val;
lz[x] *= val;
haslazy[x] = true;
return;
}
if (l>=rx || r<=lx) return;
pushdown(x);
int mid = (lx+rx)/2;
update(l, r, val, 2*x, lx, mid);
update(l, r, val, 2*x+1, mid, rx);
pushup(x);
}
void update(int l, int r, T val){
update(l, r, val, 1, 0, n);
}
T query(int l, int r, int x, int lx, int rx){
if (l<=lx && rx<=r) return tree[x];
if (l>=rx || r<=lx) return T();
int mid = (lx+rx)/2;
pushdown(x);
return query(l, r, 2*x, lx, mid) + query(l, r, 2*x+1, mid, rx);
}
T query(int l, int r){
return query(l, r, 1, 0 , n);
}
};
const int N = 2e5;
vector<int> adj[N];
int par[N], st[N], en[N], tim;
Z h[N], a[N];
void dfs(int node){
st[node] = tim++;
for (int x:adj[node]){
h[x] *= h[node];
dfs(x);
}
en[node] = tim;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;cin>>n>>q;
for (int i = 0;i<n;i++) {
cin>>a[i];
h[i] = a[i];
}
for (int i = 1;i<n;i++){
int p;cin>>p;
p--;
par[i] = p;
adj[p].push_back(i);
}
dfs(0);
SegTree<Z> seg(n);
for (int i = 0;i<n;i++) seg.add(st[i], h[i]);
seg.build();
while(q--){
int t, v;cin>>t>>v;
v--;
if (t==1){
Z x;
cin>>x;
seg.update(st[v], en[v], x/a[v]);
a[v] = x;
} else {
Z res = seg.query(st[v], en[v]);
if (v) res /= seg.query(st[par[v]], st[par[v]] + 1);
cout<<res<<'\n';
}
}
return 0;
}
Author: BitWizz
Let's try to formulate this problem in a mathematical way:
Firstly, if $$$d = 0$$$, then they will meet. If $$$x1$$$ and $$$x2$$$ both are $$$0$$$, then neither BitWizz nor Kalosmi can make a move. In such a case if $$$d \neq 0$$$, then its not possible for them to meet.
We can observe that the distance between BitWizz and Kalosmi changes by $$$\pm(x1 + x2)$$$ or $$$\pm(x1 - x2)$$$. Hence, we just need to check if there is a solution to the equation :
Replace $$$(x1 + x2)$$$ with $$$t$$$ and $$$(x1 - x2)$$$ with $$$u$$$
Now, the question reduces in checking if this $$$Diophantine$$$ $$$Equation$$$ formed has a solution or not, which is equivalent to checking if $$$d$$$ is divisible by $$$gcd(t,u)$$$ i.e if $$$d$$$ is divisible by $$$gcd(x1 + x2, abs(x1 - x2))$$$ or not.
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl "\n"
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tc;
cin >> tc;
while (tc--) {
int x1, x2, d;
cin >> x1 >> x2 >> d;
if (x1 == 0 && x2 == 0) {
if(d == 0) cout << "YES" << endl;
else cout << "NO" << endl;
} else {
if (d % __gcd(x1 + x2, abs(x1 - x2)) == 0) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}
Author: _bLIC_
answer does not depends upon the initial color and structure of the tree.
Firstly note the order of operation doesn't matter. Then, Consider a set of $$$l$$$ operations as a set having $$$l$$$ distinct vertices. If a vertex is chosen twice for a single set of operation then the result is cancelled out so, we will only consider distinct set of vertices on which operation are performed.
Two different set of vertices produce two different coloring.
Lets suppose $$$S$$$ and $$$T$$$ be two different set ($$$S\neq T$$$ but may have common vertices). Now consider running a single depth first search for both set $$$S$$$ and $$$T$$$ together in which whenever we run into vertex present in both $$$S$$$ and $$$T$$$ then, we will flip the color of all vertices in the corresponding subtree. Since $$$S\neq T$$$ there will be at least one vertex which will be present in one and not in another. During $$$\text{DFS}$$$ let $$$v$$$ be the first vertex $$$\text{WLOG}$$$ $$$v\in S$$$, $$$v\notin T$$$. Since $$$v$$$ is the first vertex to be present in only one set, each of the ancestor of $$$v$$$ were either present in both $$$S$$$ and $$$T$$$ or not present in any of $$$S$$$ or $$$T$$$. Now Color of $$$v$$$ will flip for set $$$S$$$ and won't for $$$T$$$, now the color of $$$v$$$ is different for $$$S$$$ and $$$T$$$. Also all ancestors of $$$v$$$ has already been processed so there won't be any flip in color of vertex $$$v$$$ for both set. So even after the end of $$$\text{DFS}$$$ the color of $$$v$$$ will be different, which is eventually different coloring.
Now, performing operations on same vertex twice result in cancellation of operation so if we must do exactly $$$k$$$ operation then it can be converted to number of set with size exactly $$$k$$$, $$$k-2$$$, $$$k-4$$$, $$$...$$$, $$$k\mod 2$$$.
Since each subset of vertices gives different coloring. The answer will be $$$\binom{n}{k} + \binom{n}{k-2} + \binom{n}{k-4} + ... + \binom{n}{k\mod 2}$$$, where $$$\binom{n}{k}$$$ is binomial coefficient and gives the number of subset with size exactly $$$k$$$.
#include <bits/stdc++.h>
using namespace std;
constexpr int P = 1e9+7;
using i64 = long long;
// assume -P <= x < 2P
int normalize(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int _x = 0) : x(normalize(_x)) {}
Z(i64 _x) : x(normalize(_x % P)) {}
int val() const {
return x;
}
Z operator-() const {
return Z(normalize(P - x));
}
Z inv() const {
assert(x != 0);
return power(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = normalize(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = normalize(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, Z &a) {
i64 v;
is >> v;
a = Z(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Z &a) {
return os << a.val();
}
};
const int N = 2e5+1;
Z f[N], inv[N];
void run(){
f[0] = 1;
for (int i = 1;i<N;i++) f[i] = f[i-1] * i;
inv[N-1] = f[N-1].inv();
for (int i = N-1;i>0;i--) inv[i-1] = inv[i] * i;
}
Z comb(int n, int r){
if (n<r || r<0) return Z(0);
return f[n] * inv[r] * inv[n-r];
}
void solve(){
int n, k;cin>>n>>k;
int c;
for (int i = 0;i<n;i++) cin>>c;
for (int i = 0;i<n-1;i++){
cin>>c;
}
Z ans = 0;
for (int i = k&1;i<=min(n, k);i+=2) {
ans += comb(n, i);
}
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
run();
int t;cin>>t;
while(t--){
solve();
cout<<'\n';
}
}
I – $$$n$$$ — Repetitive Infinite Sequence
Idea: JainWinn
Preparation: vrintle
Let's forget about the main problem for now, and instead let's define a function $$$f(x)$$$ as the length of string when the repetition of a certain number $$$x$$$ ends. For instance, we can take the example string from the problem,
In this, $$$f(x)$$$ values for the first $$$10$$$ numbers are: $$$1, 3, 6, 10, 15, 21, 28, 36, 45, 65$$$. How can we calculate this for a given $$$x$$$? Think of using AP sums.
Now, it's quite obvious from the problem that we can't build the whole string of numbers, so what can we do instead to find the digit? Binary Search. But binary search on what? Let's say you know that a number $$$x$$$ was being repeated where the index $$$k$$$ lies, so can we find the length of the substring from where the repetition of $$$x$$$ started till the index $$$k$$$? Yes, let's denote this by $$$m$$$, so basically $$$m = k - f(x - 1)$$$.
Now, we only have that substring of length $$$m$$$ where number $$$x$$$ is being repeated. So, we can easily find any digit at a particular index.
Time complexity: $$$\mathcal{O}(\log_{10} k \times \log_2 k)$$$
// Author: vrintle (Rahul Verma)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18
#define rep(n) for(int _ = 0; _ < n; _++)
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
void solve() {
int k;
cin >> k;
vi p10(13);
p10[0] = 1;
for(int i = 1; i < 12; i++) {
p10[i] = p10[i - 1] * 10;
}
auto sum = [&](int l, int r) {
return r * (r + 1) / 2 - l * (l - 1) / 2;
};
auto len = [&](int x) {
int p = 1, ans = 0;
while(1) {
int l = p10[p - 1], r = min(x, p10[p] - 1);
ans += sum(l, r) * p;
if(r == x) break;
p++;
}
return ans;
};
int low = 1, high = 1e8;
auto check = [&](int x) {
return len(x) >= k;
};
while(high - low > 1) {
int mid = (high + low) / 2;
if(check(mid)) high = mid;
else low = mid;
}
int x = check(low) ? low : high;
k -= len(x - 1);
k--;
string str = to_string(x);
k %= sz(str);
cout << str[k];
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
// cerr << endl;
}
return 0;
}




