Div.2
Codeforces 334A Candy Bags
Given an even n (<=100), the value of 1~n^2 sugar is distributed to n children, so that the total value of sugar in the hands of all the same.
Consider this pairing: (1,n^2), (2,n^2-1), (3,n^2-2) ... (i,n^2-i+1). Each pair of values and are n^2+1, a total of n^2/2 pairs, according to the children on the right points.
1#include <stdio.h>2 3 intMain () {4 intN;5scanf"%d",&n);6 for(inti =0, L =1, r = n * N; I < n; ++i) {7 for(intj =0; J < N/2; ++j) {8 if(j) {9Putchar (' ');Ten } Oneprintf"%d%d", l,r); AL + +; -R--; - } thePuts""); - } -}
View Code
Div.1
Codeforces 510D Fox and jumping
In fact test instructions is equivalent to finding a set of fees and the smallest cards, making ∑li*xi = 1. Where Xi is an arbitrary integer.
Then you know that for an equation ax+by=c, there is an integer solution when and only if GCD (A, b) is divisible by C. (If you don't know, you can learn to expand Euclidean algorithm).
So here from two unknowns to promote, is to find a set of fees and the smallest card, make gcd (Li) = 1.
So DP (I,J) represents the first number of I, the current GCD value is J, the minimum cost and. Although the range of J is 1e9 but the quantity is up to Nlog (a) ^2, discretization can be a bit. Where A is a numeric size.
Time Complexity O (n^2 * log (a) ^2 * log (log (a) ^2)).
1#include <bits/stdc++.h>2typedefLong LongLL;3 4 Const intN = -+5;5 Const intINF =0x3f3f3f3f;6 intN,l[n],c[n];7std::vector<int>values;8std::vector<int>DP;9 Ten int Get(intx) { One returnStd::lower_bound (Values.begin (), Values.end (), x) A-Values.begin (); - } - the voidDecompose (intx) { - for(inti =1; I*i <= x; ++i) { - if(x% i = =0) { - Values.push_back (i); +Values.push_back (x/i); - } + } A } at - intgcdintAintb) { - returnb = =0? A:GCD (b,a%b); - } - -InlinevoidTomin (int&a,intb) { in if(A > B) a =b; - } to + intMain () { -scanf"%d",&n); the for(inti =0; I < n; ++i) { *scanf"%d", L +i); $ decompose (l[i]);Panax Notoginseng } - for(inti =0; I < n; ++i) { thescanf"%d", c+i); + } A Std::sort (Values.begin (), Values.end ()); the values.erase (Std::unique (Values.begin (), Values.end ()), Values.end ()); + dp.resize (Values.size (), INF); - for(inti =0; I < n; ++i) { $ intp =Get(L[i]); $ tomin (Dp[p],c[i]); - for(intj =0; J < Dp.size (); ++j) { - if(Dp[j]! =INF) { theTomin (dp[Get(GCD (Values[j],l[i]))],c[i]+dp[j]); - }Wuyi } the } - intAnswer = dp[0]; Wu if(Answer = =INF) -Answer =-1; Aboutprintf"%d\n", answer); $ return 0; -}
View Code
The so-called daily #7-yuan introduction Pan River War male Sampathkumar Sampathkumar strong cross-river attack Lau table