"3rd Stop" National Day training Camp · Oi System simulation Game Ⅰ
With a sprint to raise the group 400 desire to come to this very small but very interesting training camp QWQ
Under the leadership of PKU Dalao began the first Oi simulation game "fried mentality's finger licking (*. >Д<) O゜ "
? Brief summary
Feel very bang ...
The first question is OK, a glance at the conclusion, so began to play the table ... I did not expect to just play a situation (why all the special case), and then cool.
The second question began to crumble. First, I thought about 20 minutes. And then found that you can not think of the positive solution, began to want to cheat points. Looked at the data ladder, found himself as if only to do the first 1/3 of the data, but also code a phala code, suddenly the whole people are not good. (Brain a group of paste) played an LCA version, just to cheat a little points.
The third question ... Even the violence did not know how to write, finally reluctantly a search ended the whole game.
The way to improve the group is still very long, you have to walk slowly ... Vehicles (°▽, °)
(What a Ghost "Chen Sun" ...) Due to internal competition, cannot provide submission platform QWQ)
? Test Questions & Analytic (a) Chen Taiyang and modulus
? topic
Give an interval [l,r] and an integer A, how many integer x, so that for each integer c∈[l,r] satisfies c%a≡c (mod x). If there is an infinity to the X that satisfies the condition, the output is 1, otherwise the number of output x.
[Size] Multiple groups of data (no more than 100 groups), all numbers are not more than 1012.
? parsing
See the title description is probably mathematical deduction. First of all, we are not unfamiliar with the modulo operation, but we can also write it in the form of a division of remainder-c mod a = b, c = ka + b (b<a). So we can get: B≡ka + B, which means b mod x = (ka + b) mod x.
Simple opening parenthesis: b mod x = ka mod x + b mod x
Move item: ka mod x = 0
So what's K? K = [C/A] ([] is rounded down)
So [C/A] * A is a multiple of x.
For each C in [L,r] satisfies the above law, so x Max is gcd ([l/a] * A, [(L + 1)/a] * A, ..., [r/a] * a) = GCD ([l/a], [ (l + 1)/A], ..., [r/a]) * A. It is easy to imagine that each factor of the maximum value of x satisfies the above-mentioned law, then the problem becomes the number of factors that seek the maximum value of x.
First because {[l/a], [(L + 1)/A], ..., [r/a]} is a continuous non-descending sequence, and the adjacent two positive integers are coprime, so if [l/a]≠[r/a], then their GCD equals 1 , otherwise they are all equal (i.e. [l/a]), then GCD is [l/a]. So that we can get the maximum value of x, so that the complexity of the square root x decomposition factor, you can obtain the answer.
The overall time complexity is O (t * sqrt (a)).
? source code
/*lucky_glass*///Chen Taiyang and modulo #include<cstdio> #include <cstring> #include <algorithm>using namespace std ; typedef long Long Ll;int main () { int t;scanf ("%d", &t); while (t--) { ll l,r,a; scanf ("%lld%lld%lld", &l,&r,&a); if (r<a) { printf ("-1"); Continue; } ll num,tot=0; if (l/a==r/a) num=l/a; else Num=a; for (ll i=1;i*i<=num;i++) { if (num%i==0) { ll a=i,b=num/i; if (a==b) tot++; else tot+=2; } } printf ("%lld", tot); } return 0;}
(b) Chen Taiyang and path
? topic
Give a tree of n nodes, for each node, find the midpoint of how many paths it is (the path has no directionality, i.e. u->v is equivalent to V->u).
[Size] n≤500000
[note] The starting and ending points of a path can be the same point
? parsing
Because of the number of statistics, and N is relatively large, it is also easy to think of the tree-shaped DP.
We first fixed 1 as the root, then for a node u, if you are the end point, then the two ends of the path are 3 cases:
① in the subtree of two different child nodes of U;
② a subtree in a child node of U, and the other is an ancestor of U;
③ a subtree in one of the child nodes of U, and another in the subtree of one of the Ancestors of U (u not in the subtree of V).
As an example:
Then we can remember from the point you go down I step can go to the number of points for g[u][i], remember from the point you first up a step, then go (random walk, as long as not go back to u) (i-1) step can go to the number of points f[u][i].
First Ask G[u][i]:
Set V is a son of U, then g[u][i] is equal to all g[v][i-1] and (go first to son V, then go down from V to i-1 step).
Then, starting with the root node, DFS will do it again.
Ask for f[u][i], need to use G:
The FA is the father of U, then f[u][i] can be understood as the first to go to the father, and then go up from the father to the ancestors, then walk from the Ancestors (i-2) step, that is f[fa][i-1], or from the Father Down (i-1) step, that is g[fa][i-1], but we can not go back to u, It is necessary to subtract the case from FA to u, which can be seen as starting from U Down (i-2) step, i.e. g[u][i-2]. So finally, you can get:
F[u][i] = F[fa][i-1] + g[fa][i-1]-g[u][i-2];
Of course we will encounter the situation of i-2<0, this time only to subtract back to u, that is, minus 1:
F[u][i] = F[fa][i-1] + g[fa][i-1]-1;
This can also be done again by DFS.
The final statistical answer can actually be found in two times DFS, the specific look at the code ...
? Source code
/*lucky_glass*///Chen Taiyang and Paths #include<cstdio> #include <cstring> #include <algorithm> #include < Vector>using namespace Std;typedef long long ll;const int n=500000;int n;vector<int> lnk[n+5];int max_dep[N+5], fa[n+5];vector<int> g[n+5],f[n+5];//down UPLL ans[n+5];void DFS1 (int u,int pre) {fa[u]=pre;for (int i=0;i<lnk[ U].size (); i++) {int v=lnk[u][i];if (v==pre) CONTINUE;DFS1 (v,u); Max_dep[u]=max (Max_dep[u],max_dep[v]);} Max_dep[u]++;g[u].resize (Max_dep[u]); F[u].resize (Max_dep[u]); g[u][0]=1;for (int i=0;i<lnk[u].size (); i++) {int V =lnk[u][i];if (V==pre) continue;for (int j=1;j<=max_dep[v];j++) {ans[u]+=1ll*g[v][j-1]*g[u][j];g[u][j]+=g[v][j-1 ];}}} void DFS2 (int u) {f[u][0]=1;if (u==1) ans[u]++;//1 ' selfelse{for (int i=1;i<max_dep[u];i++) {f[u][i]=f[fa[u]][i-1]+g [Fa[u]] [I-1];if (i>=2) F[u][i]-=g[u][i-2];else f[u][i]--;} for (int i=0;i<max_dep[u];i++) ans[u]+=f[u][i]*g[u][i];} for (int i=0;i<lnk[u].size (); i++) if (Lnk[u][i]!=fa[u]) DFS2 (Lnk[u][i]);} int main () {scanf ("%d", &n); for (int i=1,u,v;i<n;i++) {scanf ("%d%d", &u,&v); Lnk[u].push_back (v); Lnk[v].push_back (u);} DFS1 (1,0);D FS2 (1);p rintf ("%lld", Ans[1]), for (int i=2;i<=n;i++) printf ("%lld", Ans[i]); return 0;}
The EndThanks for reading!
-Lucky_glass
(Tab: If I have not clear the place can be directly in the mailbox [email protected] email me, on the weekend I will try to answer and improve the blog ~?? )
"Forward" ◇ 3rd Station ◇ National Day training Camp · OI System simulation game