Eleven Long Vacations Simulated Day1

Keywords: Algorithm Dynamic Programming

Eleven Long Vacations Simulated Day1

Eleven long holidays for five days, seven full days to tell the truth, I've been satisfied...
It makes sense to schedule two simulations in five days, since I am ready to simulate for five consecutive days...

Deliberately arrived at the computer room 7:30 a.m. before the start of 8:30 a.m....
This simulation shows that 3.5h3 questions are looser than the previous 4h4 questions

During the race:

Get the title or start traversing first
T 1 T1 T1... There's 20 violence in all directions, and then what's the rule for m==0? Take a moment and push it over 20
T 2 T2 T2... Violent Digital dp!!! More than 60 fragrances!!! The correct solution should be a generating function? Anyway, I won't...
T 3 T3 T3...... O ( n 3 ) O(n^3) O(n3) seconds out, violent modification, n^3 enumeration, over 40, positive solution might think to the segment tree?
(30 minutes now)
Then measure the difficulty of the code and decide to write T3 first
In fact, T1 might be better, but since the full arrangement left a shadow on me as detailed in the province selection, I wrote T3 first
T3 spent 15 minutes working on ideas
And then infinite RE...
I made a mistake saying I was 0%, but I couldn't find it out...
Finally, at first glance, the module seems a little close to the int boundary, and then long is not RE
deRE took 15 minutes
Sample Not Found
I went back to read the question again and found that I forgot to consider (). When can I get rid of this problem when I don't read it carefully!!!
This wasted 30 minutes, a little bit of mindset
Such a violent enumeration can't get past 20...
Rethink
Think of enumerating w and preprocessing the number of left and right (0 and 0)
This can be done O ( N M ) O(NM) O(NM) is over 60
Then each operand takes up 1 3 \frac{1}{3} 31, almost 100?
(30 minutes later)
I used something like prefix and sum when I implemented it
It took another 30 minutes to write samples and build data
1.5h left
Write T1 in full order
Given that T2 violence scores are high and digital DPS are extremely poor
I dropped the discussion of m == 0
30 min Write in full alignment Write T2
T2 Beginning Write Search for Digit dp
Write blown up...
And then you find that this is not the same as the digital dp
Writing recursion seems better
So the recursion is urgent
Finally finished writing 3 minutes before handing in the paper...
In fact, the problem of writing moments multiplied by optimization is tangible, but there is no time for QAQ
Build folder submissions as soon as possible
Expected score: 20+60+[60-100]=[120-180]

After the game:

Come back after lunch to check
WOC!!! T3 exploded zero!!!
Looking closely, I found that I forgot to think about it (maybe this is so obvious on the right side of 0)
That's great to maintain with a stack-like thing!!!
Look around at the next Duke
Like me, I could have card over 100...QAQ my 100 points!!!
This time it was a big loss to leave the question open...
First, wasting 30 minutes without examining the questions resulted in no time to write T2 moment multiplier
Then hung up another 100...
220 is rnk2!!!

Questions:

T 1 T1 T1 error algorithm:

20-point algorithm:
Enumeration Full Arrangement
Still in full handwriting arrangement?
next_permutation(a, a+n)

Is the handwriting all out of alignment? Whooping QAQ

40-point algorithm:
Pure dfs
Code simplicity is cost effective

This dfs branch has not been cut yet...

m==0
Finding the law by typing a table
Catalan number

WOC Here???

Full score algorithm
Consider that all segregations of i_p_i < 0 and i_p_i > 0 form two sequences, which must increase incrementally (why?)
This is because it takes_max(i_p_i,0) to swap all numbers of i_p_i>0 back to p_i, and it is impossible to undergo more swaps.
Since it must be two incremental sequences, dp is sufficient

Don't understand...
Code:

# include <cstdio>
# include <cstring>
# include <algorithm>
# define REP(i, n) for (int i = 1; i <= n; ++ i)
# define REP_0N(i, n) for (int i = 0; i <= n; ++ i)
# define FOR(i, a, b) for (int i = a; i <= b; ++ i)
# define CLR(a, x) memset (a, x, sizeof (a))
# define NR 310
# define mod 1000000007

using namespace std;
 
int n, m, q0, a[NR], p[NR], M;
int f[NR][NR];
 
int dfs (int i, int j)
{
	if (j < 0 || j > n) return 0;
	if (i > n) return !j;
	if (f[i][j] != -1) return f[i][j];
	
	if (p[i] == -1)
	{
		if (i + j <= m)
		{
			f[i][j] = 0;
			FOR (t, m - (i + j - 1), n) {
        f[i][j] += dfs (i + 1, j + t);
        f[i][j] %= mod;
      }
		}
		else
		{
			f[i][j] = dfs (i + 1, j);
			REP (t, n) {
        f[i][j] += dfs (i + 1, j + t);
        f[i][j] %= mod;
      }
			if (i > M) {
        f[i][j] += dfs (i + 1, j - 1);
        f[i][j] %= mod;
      }
		}
	}
	else
	{
		if (p[i] == i + j) f[i][j] = dfs (i + 1, j);
		else if (p[i] < i + j) f[i][j] = dfs (i + 1, j - 1);
		else f[i][j] = dfs (i + 1, j + p[i] - (i + j));
	}
	return f[i][j];
}
 
int main ()
{
  freopen("wrong.in","r",stdin);
  freopen("wrong.out","w",stdout);
	for (scanf ("%d", &q0); q0; -- q0)
	{
		scanf ("%d%d", &n, &m), M = 0;
		int lst = 0; CLR (f, -1);
		int ci = 1; bool valid = true;
		REP (i, n) p[i] = -1;
		REP (i, m) 
		{
			scanf ("%d", &a[i]), p[a[i]] = i;
			if (a[i] == ci) for (++ ci; p[ci] != -1 && ci <= n; ++ ci) ;
			else if (a[i] < lst) valid = false;
			else lst = a[i];
			M = max (a[i], M);
		}
		if (!valid) {puts ("0"); continue;}
		printf ("%d\n", dfs (1, 0));
	}
	return 0;
}

T 2 T2 T2 Geely number:

60-point algorithm:
order a n a_n An is such a n-digit number that each number has an odd number, and 1 and 3 occur even times.
order b n b_n bn is such a n-digit number that each number has an odd number, one occurs odd times and three occurs even times. (By symmetry, it can also mean that three occurs odd times and one occurs even times)
order c n c_n cn is such a number of n digits that each digit has an odd number, with 1 and 3 appearing odd numbers
a n a_n an​= 3 a n − 1 a_n−1 an​−1 + 2 b n − 1 b_{n−1} bn−1​
b n b_n bn​= 3 b n b_n bn​−1 + c n − 1 c_{n−1} cn−1​ + a n − 1 a_{n−1} an−1​
c n c_n cn​ = 3 c n c_n cn​−1 + 2 b n − 1 b_{n−1} bn−1​
Recursion is 60 minutes
Full score algorithm:
Doubling speed
Matrix Multiplication
General term formula
......
Exponential generation function
(e^ x) ^ 3( e x + e − x 2 \frac{e^ x+e^ −x}{2} 2ex+e−x​−1)^2

After the game I pushed out the transfer matrix myself...
Exponential generation function roll, moment multiplied by yyds!!!
Code:

#include<bits/stdc++.h>
#define ll long long
#define int ll
using namespace std;
const int maxn=10,mod=1e9+7;
struct Matrix
{
	ll mat[maxn][maxn];
}f,base,ans;
inline ll read()
{
	ll ret=0;char ch=' ',c=getchar();
	while(!(c<='9'&&c>='0')) ch=c,c=getchar();
	while(c<='9'&&c>='0') ret=(ret<<1)+(ret<<3)+c-'0',c=getchar();
	return ch=='-'?-ret:ret;
}
void init() {for(int i=1;i<=9;i++) for(int j=1;j<=9;j++) base.mat[i][j]=(i==j);}
inline Matrix operator*(Matrix &a,Matrix &b)
{
	Matrix ret;
	memset(ret.mat,0,sizeof(ret.mat));
	for(int i=1;i<=9;i++)
		for(int k=1;k<=9;k++)
			for(int j=1;j<=9;j++)
				(ret.mat[i][j]+=a.mat[i][k]*b.mat[k][j])%=mod;
	return ret;
}
Matrix qpow(Matrix a,ll b)
{
	Matrix res=base;
	while(b)
	{
		if(b&1) res=res*a;
		a=a*a;
		b>>=1;
	}
	return res;
}
signed main()
{
	int n=read();
	f=(Matrix){{{0},
	{0,3,0,0,0,0,0,0,0,0},
	{0,1,3,1,0,0,0,0,0,0},
	{0,0,1,3,0,0,0,0,0,0},
	{0,1,0,0,3,1,0,0,0,0},
	{0,0,0,0,1,3,0,0,0,0},
	{0,0,1,0,1,0,3,1,1,0},
	{0,0,0,0,0,1,1,3,0,1},
	{0,0,0,1,0,0,1,0,3,1},
	{0,0,0,0,0,0,0,1,1,3}}};
	ans=(Matrix){{
	{0},
	{0,3,0,0,0,0,0,0,0,0},
	{0,1,0,0,0,0,0,0,0,0},
	{0},
	{0,1,0,0,0,0,0,0,0,0},
	{0},{0},{0},{0}}};
	init();
	Matrix tmp=qpow(f,n-1);
	// for(int i=1;i<=9;i++)
	// {
		// for(int j=1;j<=9;j++) cout<<tmp.mat[i][j]<<" ";
		// cout<<endl;
	// }
	// cout<<endl;
	ans=tmp*ans;
	// for(int i=1;i<=9;i++)
	// {
		// for(int j=1;j<=9;j++) cout<<ans.mat[i][j]<<" ";
		// cout<<endl;
	// }
	printf("%lld",ans.mat[9][1]);
	return 0;
}

T 3 T3 T3(0w0)

For 20% of the data, 1 < n, m < 50;
Art of Great Violence
When asked, enumerate 5 characters to determine if (0w0)
O(m*C(n,5)) For 40% of the data, 1 < n, m < 200; less violent violence
When asked, enumerate where the middle character w is, how many zeros are on the left (0, how many zeros are on the right), how many O (m*C(n, 3)) are the answers centered on the current w by multiplying the principles
For 60% of the data, 1 < n, m < 1000;
Ask DP each time:
f[i][j] indicates how many sub-sequences in the first I-bit of an interval are equal to the first j-bit of (0w0) (j=0...5)
Transfer:
f[i+1][j] += f[i][j] (bit i+1 is not selected)
f[i+1][j+1] += f[i][j] (i f bit i+1 of the sequence equals bit j+1 of the face text)
Classic LCS practices
O(nm)
For 100% of the data, 1 < n, m < 50000
How many (0w0) within the maintenance interval of the segment tree
There are two types of answers within the interval:
Not across the midline: left and right subintervals add up
Across the midline: there are four types:
(0w0 on the left,) on the right
(0w on left, 0) on right
(0 on the left, w0) on the right
(on the left, 0w0) on the right
So you also have to maintain how much of each interval is ((0, (0w, (0w0, 0w0), 0)) at the same time.
Recursive
Each interval is marked with an f[i][j], which indicates how many times the sub-string formed by the i~j bits of Yan Text occurs
Update interval: f[i][j] = fl[i][j]+fr[i][j]+fl[i][k] * fr[k+1][j]
fl,fr corresponds to values maintained by left and right subintervals
Interval modification? Marking, good maintenance
O (nlogn*53) is actually only O(nlogn*22)

The exact solution is really a segment tree...
100 points violence yyds!!!
Code:

#include <cstdio>
#include <cstring>

#define N 50055
#define getch for (ch=getchar(); ch<=32; ch=getchar())

char s[N];
int f[N*4][5][5],ans[5][5],tmpans[5][5],len[N*4],cv[N*4],n,m,ch,x,y;

void read(int &x)
{
    getch;
    for (x=0; ch>32; ch=getchar()) x = x*10+ch-48;
}

void merge(int src1[5][5], int src2[5][5], int dst[5][5])
{
    int i,j,k;
    for (i=0; i<=4; ++i)
    for (j=i; j<=4; ++j)
    {
        dst[i][j] = src1[i][j]+src2[i][j];
        for (k=i; k<j; ++k) dst[i][j] += src1[i][k]*src2[k+1][j];
    }
}

void build(int l, int r, int t)
{
    len[t] = r-l+1;
    if (l == r)
    {
        if (s[l] == '(') f[t][0][0] = 1;
        else
        if (s[l] == '0') f[t][1][1] = f[t][3][3] = 1;
        else
        if (s[l] == 'w') f[t][2][2] = 1;
        else
        if (s[l] == ')') f[t][4][4] = 1;
        return;
    }
    int k = l+r>>1;
    build(l, k, t*2);
    build(k+1, r, t*2+1);
    merge(f[t*2], f[t*2+1], f[t]);
}

void downtag(int t)
{
    if (cv[t] == 0) return;
    if (t*2 < N*4) cv[t*2] = cv[t*2+1] = cv[t];
    int i,j;
    memset(f[t], 0, sizeof(f[t]));
    if (cv[t] == '(') f[t][0][0] = len[t];
    else
    if (cv[t] == '0') f[t][1][1] = f[t][3][3] = len[t];
    else
    if (cv[t] == 'w') f[t][2][2] = len[t];
    else
    if (cv[t] == ')') f[t][4][4] = len[t];
    cv[t] = 0;
}

void modify(int l, int r, int t)
{
    if (x<=l && r<=y)
    {
        cv[t] = ch;
        downtag(t);
        return;
    }
    downtag(t*2), downtag(t*2+1);
    int k = l+r>>1;
    if (x <= k) modify(l, k, t*2);
    if (y > k) modify(k+1, r, t*2+1);
    merge(f[t*2], f[t*2+1], f[t]);
}

void query(int l, int r, int t)
{
    if (x<=l && r<=y)
    {
        merge(ans, f[t], tmpans);
        memcpy(ans, tmpans, sizeof(tmpans));
        return;
    }
    downtag(t*2), downtag(t*2+1);
    int k = l+r>>1;
    if (x <= k) query(l, k, t*2);
    if (y > k) query(k+1, r, t*2+1);
}

int main()
{
    scanf("%d%d", &n, &m);
    scanf("%s", s+1);
    build(1, n, 1);
    while (m--)
    {
        getch;
        if (ch == 'A')
        {
            read(x);
            y = x;
            getch;
            modify(1, n, 1);
        }
        else
        if (ch == 'B')
        {
            read(x), read(y);
            getch;
            modify(1, n, 1);
        }
        else
        if (ch == 'C')
        {
            read(x), read(y);
            memset(ans, 0, sizeof(ans));
            query(1, n, 1);
            printf("%u\n", ans[0][4]);
        }
    }
    return 0;
}


Oh, yes, the caption also wrote this sentence:

Module 4294967296?
4294967296=232
With int memory, let it overflow naturally during operation
Output in unsigned int format

The case has been solved...
I (what sounds good) you (what sounds good)
md wastes me 15 minutes

Posted by JMair on Mon, 04 Oct 2021 10:16:33 -0700