Part of UPC game 46
There are many simple questions in this game. You can get A by basically understanding the meaning of the question
A: Xiao Kai's doubts (number theory)
When A and B are coprime, what is the maximum number that A and B cannot express?
Play the watch and find the law... The conclusion is:
a
n
s
=
a
∗
b
−
a
−
b
ans=a*b-a-b
ans=a∗b−a−b
B: Ireducible polynomial (number theory)
In mathematics, a polynomial is an expression consisting of variables (also called indeterminate) and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponents of variables. For example, x2 + 4x + 7.
A polynomial is said to be irreducible if it cannot be factored into two or more non-trivial polynomials with real coefficients.
For example, x2+1 is irreducible, but x2-2x+1 is not (since x2-2x+1=(x-1)(x-1)).
Given a polynomial of degree with integer coefficients: anxn+an−1xn−1+...+a1x+a0, you need to check whether it is irreducible or not.
Give you a polynomial and ask if you can factorize it into real numbers.
In fact, this is also a conclusion. In the real number field, there are only two kinds of inseparable Polynomials: first-order polynomials and second-order polynomials (△ < 0).
c o d e : code: code:
#include<stdio.h> #include<stdlib.h> #include <bits/stdc++.h> #define ll long long using namespace std; // typedef struct Node *List; ll a[10101]; int main() { int n,t; cin >> t; while(t--) { cin >> n; for(int i=1;i<=n+1;i++) { cin >> a[i]; } if(n>2) printf("No\n"); else if(n==1) printf("Yes\n"); else { ll t=a[2]*a[2]-4*a[1]*a[3]; if(t<0) { printf("Yes\n"); } else { printf("No\n"); } } } return 0; }
C: Distance pastures
Farmer John's farm is made up of an N x N grid of pastures, where each pasture contains one of two different types of grass. To specify these two types of grass, we use the characters ( and ), so for example FJ's farm might look like the following grid:
(())
)()(
)(((
))))
When Bessie the cow travels around the farm, it takes her A units of time to move from a pasture to an adjacent pasture (one step north, south, east, or west) with the same grass type, or B units of time to move to an adjacent pasture with a different grass type. Whenever Bessie travels from one pasture to a distant pasture, she always uses a sequence of steps that takes the minimum amount of time. Please compute the greatest amount of time Bessie will ever need to take while traveling between some pair of pastures on the farm.
Given an n × A grid of N, each grid has a character, either '(', or ')'. Each grid is adjacent to its upper, lower, left and right grids. For the two adjacent grids x and y, if the characters in x and y are the same, it will consume a unit time. If the characters in x and y are different, it will consume B unit time. Define the time from point S to point T as D (S, T). Now I want you to find the largest D (S, T) in the grid.
In the old way, take a and b as the weights of points in different situations, and then run spfa mindlessly. Enumerate the starting point and ending point to run spfa n*n times. Remember to require the maximum value of dis array every time and update it constantly.
c o d e : code: code:
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn=5500,mod = 998244353; inline int read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } inline void write(int x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'+' '); } struct node { int x,y; }; ll ans,dis[102][102]; bool mp[102][102]; int n,a,b; char s[102][110]; int dx[4]={0,0,-1,1}; int dy[4]={-1,1,0,0}; void spfa(int x,int y) { queue<node>q; memset(dis,0x3f3f3f3f3f,sizeof(dis)); memset(mp,0,sizeof(mp)); mp[x][y]=1; q.push((node){x,y}); dis[x][y]=0; while(!q.empty()) { node d=q.front(); q.pop(); mp[d.x][d.y]=0; for(int i=0;i<4;i++) { int sx=d.x+dx[i]; int sy=d.y+dy[i]; if(sx>0&&sx<=n&&sy>0&&sy<=n) { int t1; if(s[d.x][d.y]==s[sx][sy]) { t1=a; } else { t1=b; } if(dis[sx][sy]>dis[d.x][d.y]+t1) { dis[sx][sy]=dis[d.x][d.y]+t1; if(mp[sx][sy]==0) { mp[sx][sy]=1; q.push((node){sx,sy}); } } } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { ans=max(ans,dis[i][j]); } } } int main() { cin >> n >> a >> b; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cin >> s[i][j]; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { spfa(i,j); } } cout << ans; return 0; }
F: Peaks (graph theory)
There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is Hi. There are also M roads, each connecting two different observatories. Road j connects Obs. Aj and Obs. Bj.Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road. Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.
How many good observatories are there?
There are n points and M undirected edges. Given the weights of these n points and the points connected by these m edges
Find how many points have greater weights than all points it can reach through only one edge
Constraints
·2≤N≤10^5
·1≤M≤10^5
·1≤Hi≤10^9
·1≤Ai,Bi≤N
·Ai≠Bi
·Multiple roads may connect the same pair of observatories.
·All values in input are integers.
The basic problem of graph theory, chain forward star construction, and then run the cycle to judge the statistical times.
c o d e : code: code:
#include<stdio.h> #include<stdlib.h> #include <bits/stdc++.h> #define ll long long using namespace std; // typedef struct Node *List; int n,m,ans,a[202202],head[202202],cnt,x,y; struct node { int to,next; }e[401101]; void add(int u,int v) { e[++cnt]=(node){v,head[u]}; head[u]=cnt; } int main() { cin >> n >> m; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=m;i++) { scanf("%d %d",&x,&y); add(x,y); add(y,x); } for(int i=1;i<=n;i++) { int fl=0; for(int j=head[i];j;j=e[j].next) { if(a[i]<=a[e[j].to]) { fl=1; break; } } if(!fl) ans++; } cout << ans; return 0; }
I: This Message Will Self-Destruct in 5s(map)
You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.
There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is Ai.
According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.
The absolute difference of their attendee numbers is equal to the sum of their heights.There are N(N−1)/2 ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?
Given a sequence AA with length nn, find how many groups ii,jj satisfy:
- i<j
- j − i = a [ i ] + a [ j ] j-i=a[i]+a[j] j−i=a[i]+a[j]
- P.S.: We cannot let you know the secret.
Constraints
·All values in input are integers.
·2≤N≤2×10^5
·1≤Ai≤10^9 (1≤i≤N)
Switch j − a [ j ] = i + a [ i ] j-a[j]=i+a[i] j − a[j]=i+a[i], just scan the map for statistics.
c o d e : code: code:
#include<stdio.h> #include<stdlib.h> #include <bits/stdc++.h> #define ll long long using namespace std; // typedef struct Node *List; map<int,int>mp1; ll a[201015],ans,n; int main() { cin >> n; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } mp1[a[1]+1]++; for(int i=2;i<=n;i++) { ll k=i-a[i],k2=a[i]+i; ans+=mp1[k]; mp1[k2]++; } cout << ans; return 0; }
50: Typo (stack)
Bessie has just purchased a new laptop computer, but she unfortunately finds herself unable to type well, given the size of her large hooves relative to the small keyboard. Bessie has just attempted to type in one of her favorite patterns – a balanced string of parentheses. However, she realizes that she might have mis-typed one character, accidentally replacing ( with ) or vice versa. Please help Bessie compute the number of locations in the string such that reversing the single parenthesis at that location would cause the entire string to become balanced.
There are several ways to define what it means for a string of parentheses to be "balanced". Perhaps the simplest definition is that there must be the same total number of ('s and )'s, and for any prefix of the string, there must be at least as many ('s as )'s. For example, the following strings are all balanced:
()
(())
()(()())
while these are not:
)(
())(
((())))
Misspelled a bracket and asked to change a bracket to make the sequence legal.
We use the stack to match the parentheses, see if there are two elements left in the stack, and judge the type of parentheses out of the stack. If it is an left parenthesis, look for the left parenthesis on its right. If it is a right parenthesis, look for the right parenthesis on its left.
c o d e : code: code:
#include<iostream> #include<iomanip> #include<cstring> #include<cmath> #include<cstdio> #include <vector> #include <set> #include<algorithm> #include <bits/stdc++.h> #define ll long long using namespace std; const int maxn=5500,mod = 998244353; inline int read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } inline void write(int x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'+' '); } char s[100001]; stack<int>st; ll ans; int main() { scanf("%s",s+1); int n1=strlen(s+1); for(int i=1;i<=n1;i++) { if(st.empty()) { st.push(i); } else { if(s[i] == '(') { st.push(i); } else if(s[i] == ')') { if(s[st.top()] == '(') st.pop(); else st.push(i); } } } if(st.size()==2) { if(s[st.top()]==')') { int r=st.top(); for(int i=0;i<r;i++) { if(s[i]==')') { ans++; } } } else { int r=st.top(); for(int i=r;i<=n1;i++) { if(s[i]=='(') { ans++; } } } } cout << ans; return 0; }
M: Tower
There are N blocks numbered 1,2,..., N. For each i (1 ≤ i ≤ N), the weight of block i is wi, the firmness is si, and the value is vi.
Tarot decided to build a tower, select some from N wood blocks and stack them vertically in a certain order. Here, the tower must meet the following conditions:
For each block i contained in the tower, the total weight of the stacked blocks above it shall not be greater than si.
Finds the sum of the maximum possible values of the blocks contained in the tower.
This question needs to be sorted. How do you sort it? Obviously, what we want is that for I and j, it will not be worse to turn them around.
a
s
−
b
w
>
b
s
−
a
w
a_s-b_w>b_s-a_w
as−bw>bs−aw
We define DP[I] as the maximum value when how much is left. The equation is:
f[min(j-a[i].w,a[i].s)]=max(f[min(j-a[i].w,a[i].s)],f[j]+a[i].v);
c o d e : code: code:
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define LL long long using namespace std; LL f[10010]; struct node{int w,s,v;}a[1010]; int n,m; bool cmp(node a,node b) {return a.s-b.w>b.s-a.w;} int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d %d %d",&a[i].w,&a[i].s,&a[i].v); sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) { for(int j=a[i].w;j<=10000;j++) f[min(j-a[i].w,a[i].s)]=max(f[min(j-a[i].w,a[i].s)],f[j]+a[i].v); f[a[i].s]=max(f[a[i].s],(LL)a[i].v); } LL ans=0; for(int i=0;i<=10000;i++) ans=max(ans,f[i]); printf("%lld",ans); }