Codeforces Round #178 (Div. 2)

Source: Internet
Author: User

Question C:

Very interesting. I will give you m positions. Now I want to extend them from these m positions to the two sides of the positions. I will ask you how many methods you can extend all the numbers to in total.

The possible number of those numbers in the middle of the two locations should be 2 ^ (k-1), because each time there are two options, either left Endpoint or right endpoint.

Then, an arrangement is obtained from all the intervals, namely, a1 a2 a3 b1 b2 b3 c1 c2 c3. Now, we want to find the total number of the sequences, which requires that the order of the same type cannot be changed, it is easy to find that this is actually similar to the arrangement of multiple sets, ans = n! /(P1! * P2! * P3 !) And then the numerator multiplied by a 2 ^ (k-1 ).

 

# Include <cstdio>
# Include <vector>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Typedef long lld;
Const int inf = ~ 0u> 2;
Const int mod = 1000000007;
Const int maxn = 1010;
Int pos [maxn];
Lld Pow (lld a, lld B ){
Lld ans = 1;
While (B ){
If (B & 1) ans = ans * a % mod;
A = a * a % mod;
B> = 1;
}
Return ans;
}
Lld fac [maxn], two [maxn];
Vector <int> rec;
Void solve (int n ){
Fac [0] = 1; two [0] = 1;
For (int I = 1; I <= 1000; I ++) fac [I] = fac [I-1] * I % mod;
For (int I = 1; I <= 1000; I ++) two [I] = two [I-1] * 2% mod;
Int sum = 0;
For (int I = 0; I <rec. size (); I ++) sum + = rec [I];
// Printf ("sum = % d \ n", sum );
Lld ans = fac [sum];
For (int I = 0; I <rec. size (); I ++ ){
Ans = ans * Pow (fac [rec [I], mod-2) % mod;
}
For (int I = 1; I <rec. size ()-1; I ++ ){
If (rec [I]> 0) ans = ans * two [rec [I]-1] % mod;
// Printf ("ans = % d \ n", rec [I]);
}
Printf ("% I64d \ n", ans );
}

Int main (){
Int n, m;
Scanf ("% d", & n, & m );
For (int I = 0; I <m; I ++ ){
Scanf ("% d", & pos [I]);
}
Sort (pos, pos + m );
Rec. push_back (pos [0]-1 );
For (int I = 1; I <m; I ++ ){
Rec. push_back (pos [I]-pos [I-1]-1 );
}
Rec. push_back (n-pos M-1]);
Solve (n );
Return 0;
}

 


D:

Last time I had a question about grid rebound ....

The rebound character in the grid is that the parity of the lattice passed by the 45-degree reflection is the same, so no matter how the rebound, it will not pass through two adjacent squares.

I will give you a n * m rectangular grid and ask you how many grids you can pass from a certain position and direction to dye the grid into a checkerboard, that is, the color of each two adjacent grids is different.

Each rectangle has two kinds of checkerboard, no matter which one, if successfully dyed one, will certainly pass through the boundary grid n + m-2 times, that is, if the rebound process through the n + m-2 a different boundary mesh, certainly corresponds to a board scheme, if two reach the same position in the same direction, it must have entered a loop.


E:

Give you a tree with 5000 points. Each side has edge weight. You can delete an edge and change it to another position. Make sure it is still in the shape of the tree after the change, now, we will ask you the minimum sum of distance between all the points.

5000 points, now think of n ^ 2 .. then, the brute-force enumeration removes each edge, and the rest is the two sides, and then reconnects an edge. Just think about it, this edge must be the center of gravity of the two sides, the next thing is simple...

I saw the wrong question during the competition and thought that the question could be modified many times... Then there are a variety of YY, not careful enough...

 

 


--------------------------------------------------------------------------------

# Include <cstdio>
# Include <vector>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Const int inf = ~ 0u> 2;
Const int mod = 1000000007;
Const int maxn = 5010;
Int son [maxn];
Long dp [maxn];
Int n;
Int mp [maxn] [maxn];
Vector <int> edge [maxn];
Int node;
Struct Edge {
Int s, t, w;
Edge (){}
Edge (int a, int B, int c)
{
S =;
T = B;
W = c;
}
} In [maxn];
Int dep [maxn];
Void dfs (int u, int f ){
Son [u] = 1;
Dep [u] = dep [f] + 1;
Int sz = edge [u]. size ();
For (int I = 0; I <sz; I ++ ){
Int v = edge [u] [I];
If (v = f) continue;
Dfs (v, u );
Son [u] + = son [v];
}
}
Int cen;
Int S [maxn];
Void DFS (int u, int f ){
S [u] = 1;
Int sz = edge [u]. size ();
For (int I = 0; I <sz; I ++ ){
Int v = edge [u] [I];
If (v = f | v = node) continue;
DFS (v, u );
S [u] + = S [v];
}
}
Long sum;
Void dfs1 (int u, int f, int rt ){
Dp [u] = 0;
Int sz = edge [u]. size ();
For (int I = 0; I <sz; I ++ ){
Int v = edge [u] [I];
If (v = f | v = node) continue;
Dfs1 (v, u, rt );
Sum + = (long) S [v] * (S [rt]-S [v]) * mp [u] [v];
Dp [u] + = dp [v] + (long) S [v] * mp [u] [v];
}
}
Long Mi;
Void dfs2 (int u, int f, int rt ){
If (u = rt ){
Mi = min (dp [u], Mi );
} Else {
Long tmp = dp [f]-dp [u]-(long) S [u] * mp [f] [u];
Dp [u] + = (long) mp [f] [u] * (S [rt]-S [u]) + tmp;
Mi = min (dp [u], Mi );
}
Int sz = edge [u]. size ();
For (int I = 0; I <sz; I ++ ){
Int v = edge [u] [I];
If (v = f | v = node) continue;
Dfs2 (v, u, rt );
}
}
Int main (){
Scanf ("% d", & n );
Int a, B, c;
Int tot = 0;
For (int I = 1; I <n; I ++ ){
Scanf ("% d", & a, & B, & c );
Edge [a]. push_back (B );
Edge [B]. push_back ();
Mp [a] [B] = mp [B] [a] = c;
In [tot ++] = Edge (a, B, c );
}
Dfs (1, 0 );
Long INF = (long) 1000000000 * (long) 1000000000;
Long ans = INF;
For (int I = 0; I <tot; I ++ ){
Int u = in [I]. s, v = in [I]. t;
If (dep [u]> dep [v]) swap (u, v );
Node = v;

Mi = INF; sum = 0;
DFS (1, 0 );
Dfs1 (1, 0, 1 );
Dfs2 (1, 0, 1 );

Long tmp = (long) (son [1]-son [node]) * (son [node]) * in [I]. w;
Tmp + = Mi * (son [node]);
Tmp + = sum;
Mi = INF; sum = 0;
DFS (node, u );
Dfs1 (node, u, node );
Dfs2 (node, u, node );

Tmp + = Mi * (son [1]-son [node]);
Tmp + = sum;
If (tmp <ans) ans = tmp;
}
Printf ("% I64d \ n", ans );
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.