Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

### chokudai's blog

By chokudai, history, 7 months ago, ,

We will hold AtCoder Beginner Contest 147.

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

• +11

 » 7 months ago, # |   +16 Can anyone please give a solution to F?
•  » » 7 months ago, # ^ | ← Rev. 3 →   0 The problem actually asks how many different sums can be got from all subsets.Let $Y = X - D$. Then $A_i=Y+iD$. We can divide $gcd(Y, D)$ from $Y$ and $D$, now let's assume $gcd(Y,D)=1$.We can maintain n maps. Enumerate how many elements we choose from the sequence, now assume it's k.So the sum of elements we picked from sequence should be $kY+bD$, you can store all the sums possible if we pick k elements into map $(k\mod D)$.If there are different ways to choose elements with the same sum, then they will be stored into same map.Now let's consider $k$-th maps, it stores sum like $(k+tD)Y+bD$, if we subtract $kY$ from them, they will be multiple of $D$.Consider all sums picked $k$ elements, actually the sum should be $\{kY+Dv|1+\ldots+k\leq v\leq (n-k+1)+\ldots+n\}$You can subtract $(k\mod D)Y$ from them, then divide $D$, you'll find all sums contribute from picked $k$ elements will be compressed as a continuous interval $[l,r]$, you can maintain them with map.At last, count the total length of all intervals stored in maps. The total time complexity is $O(n\log_2n)$.
•  » » 7 months ago, # ^ | ← Rev. 2 →   +11 F:Let $AP = A + (A + 1\cdot D) + (A + 2\cdot D) + \dots + (A + (N - 1)\cdot D$)The problem essentially asks to count unique sums of every subset of $AP$.Let's iterate over $l$, in which we consider subsets of size $l$.For each $l$, we can find a range $[L, R]$, such that each sum of subsets of size $l$, lies in $[L, R]$.It is easy to see that $L = A\cdot l + \sum\limits_{i=0}^{l-1}{i\cdot D}$Similarly, $R = A\cdot l + \sum\limits_{i=n-l}^{n-1}{i\cdot D}$By taking $l$ leftmost, and $l$ righmost numbers respectively.It can be proven that for each such range, all sums with intervals of $D$ are possible to obtain, i.e. $\left(L, L + D, \dots, R\right)$Since, there can overlapping of sums in ranges for those $l$'s for which $L \pmod D$ are same, we merge such ranges before adding there contribution.Also, handle the case for $D = 0$ separately.Link
•  » » » 7 months ago, # ^ |   0 Thanks, it was a beautiful problem.
•  » » » 7 months ago, # ^ |   0 Awesome explanation,can I understand your logic It can be proven that for each such range, all sums with intervals of D are possible to obtain,but unable to think of a proof of it.Kindly explain
 » 7 months ago, # |   0 Can anyone explain me the solution for problem C ?
•  » » 7 months ago, # ^ | ← Rev. 4 →   0 The editorial is out! LinkYou can use Google translate for the pdf file
•  » » » 7 months ago, # ^ |   0 Actually no need of translation.The editorials are first explained in Japanese and then below they were explained in English also.
•  » » 7 months ago, # ^ |   +1 C. If you decide whether each person is honest or unkind, you can easily check in O (N2) whether the condition is consistent with the testimony. Since there are only 2N ways to determine whether or not each person is honest, it is sufficient to investigate whether or not there is a contradiction in all these cases, and to answer the largest number of honesty before the contradiction occurs. The above shows that an integer j between 0 and 2N is for an integer k between 1 and N, person k is honest and that the k-1 digit is 1 when j is represented in binary. By defining it as equivalent state, you can easily try all cases, this method is called bit traversal.
•  » » » 7 months ago, # ^ |   0 For me it was not that easy, in fact I did not solve it in time. The check for contradictions looks kind of fiddly to me.Somewhere in my loop over (1<>k)&1)) ok=false; 
•  » » » » 7 months ago, # ^ |   0 The similar lines from the editorial look even worse. I would like to find ways to implement this less error prone.  for(int j = 1; j <= A[i]; j++) { if(((bits >> (x[i][j]-1)) & 1) ^ y[i][j]) ok = false; } 
•  » » » » 7 months ago, # ^ |   0 https://atcoder.jp/contests/abc147/submissions/8862393 Here is a link to my submission for problem C, I used bitmasking along with basic recursion for brute forcing whether the solution exist or not you can have a look at it.
•  » » » » » 7 months ago, # ^ |   0 Perhaps mine is simpler? https://atcoder.jp/contests/abc147/submissions/8860462
•  » » 7 months ago, # ^ |   +3 Since N is at most 15, the main idea is to generate all 2^15 = 32768 possible combinations of whether one is honest, and test whether there are any contradictions. I did this using bit operations, but of course there are other ways of doing that.My submission: https://atcoder.jp/contests/abc147/submissions/8866706
 » 7 months ago, # |   +2 D. An Efficient Solution can solve this problem in O(n*64) or O(n) time. The assumption here is that integers are represented using 64 bits. Optimized solution will be to try bit manipulation. To implement the solution, we consider all bits which are 1 and which are 0 and store their count in two different variables. Next multiple those counts along with the power of 2 raised to that bit position. Do this for all the bit positions of the numbers. Their sum would be our answer.Explanation : arr[] = { 7, 3, 5 } 7 = 1 1 1 3 = 0 1 1 5 = 1 0 1 For bit position 0 : Bits with zero = 0 Bits with one = 3 Answer = 0 * 3 * 2 ^ 0 = 0 Similarly, for bit position 1 : Bits with zero = 1 Bits with one = 2 Answer = 1 * 2 * 2 ^ 1 = 4 Similarly, for bit position 2 : Bits with zero = 1 Bits with one = 2 Answer = 1 * 2 * 2 ^ 2 = 8 Final answer = 0 + 4 + 8 = 12
•  » » 7 months ago, # ^ | ← Rev. 2 →   0 Isn't it that we need to divide by 2 somewhere?The above calculates sum of all pairs, not only the ones where j>i. Or am I wrong?
•  » » 7 months ago, # ^ |   0 When you paste the whole explanation from geeksforgeeks !!! the whole solution was available on gfg They should not add such question the contest when the solution is already available on such educational websites
•  » » » 7 months ago, # ^ |   0 atcoder.jp/contests/abc147/submissions/8856735 No copy pasting have a look at the submission
•  » » » » 7 months ago, # ^ |   0 Bro, I didn't say that your solution is copy-paste from GeeksforGeeks I wrote that the explanation you gave is direct copy-paste from GeeksforGeeks Take Chill !!!
•  » » » » » 7 months ago, # ^ |   0 Ok sorry bro I took it the wrong way
•  » » » » » » 7 months ago, # ^ |   0 Though you will definitely become a candidate master one day !!! :)
•  » » » » » » » 7 months ago, # ^ |   0 Thanks X-D :-)
•  » » 7 months ago, # ^ |   +2 here for D solid explanation link
 » 7 months ago, # |   0 geothermal didn't write editorials this time.!
•  » » 7 months ago, # ^ |   0 I was also waiting for his editorial. His explanation of the problems are best
 » 7 months ago, # |   0 English editorial please.
 » 7 months ago, # |   0 Does anyone have a problem with pD?My answer to sample 3 is 794304820, but I somehow got an AC.My code
•  » » 7 months ago, # ^ |   0 I had the same problem! Where I was making a mistake was calculating 2^60, I wasn't taking the modulo 1e9+7 during its computation assuming it to fit in a long long.
 » 7 months ago, # | ← Rev. 2 →   0 I had an idea for F but couldn't implement in time : The question reduces to finding distinct values S can take. So I did some manual work for number of terms used to form S, say i, so for each i consecutive distinct values lie at the difference of D(common difference), now we have different intervals for all i, need to combine them. To combine first I sort intervals, standard sorting and include a value called as base value with each interval (which is the minimum offset from zero we get if we keep reducing the minimum value of interval ) and then I partition intervals on basis of offset value into different categories of intervals. Now the question reduces to merging of intervals for each category and finding total elements present in all resultant intervals at gap of d.
 » 7 months ago, # | ← Rev. 4 →   +4 E was a rather standard knapsack DP.We can find the optimal path with a boolean matrix of size $H\times W \times 6400$ that has $true$ on index $(i,j,d)$ iff it's possible to reach the square $(i,j)$ while having total absolute (red — blue) difference $d$. I'd say the only insight needed here was to notice that you only need $6400$ states for each square. This is because each path has length $H+W-1 < 160$, and the maximum $\vert A_{i,j} - B_{i,j}\vert$ to be added/subtracted in each step is $80$, so an optimal path will never reach over $6400$ or below $-6400$ at any point.Code
•  » » 6 months ago, # ^ |   0 How could you be sure that difference will never reach 6400?
 » 7 months ago, # |   +7
 » 7 months ago, # | ← Rev. 2 →   0 I thought of C in terms of bipartite graph.Can someone share if he had similar approach and having AC? Code
 » 7 months ago, # |   0 I got a question, comparing both submissions, which are pretty similar, Code1 WA and Code2 AC I know that the first one has indices out of bounds and it might lead to undefined behavior, but it is ok that the verdict is a WA or what else could it be ? I mean I do not get why the code1 is a WA, the only idea I have is regarding undefined behavior but is there any other possibility regarding the WA?Thank you
 » 7 months ago, # |   +1 Can anyone explain how to solve E in O(min{H, W}(H + W)M), M being Let M = max{|A11 − B11|, ..., |AHW − BHW |}? The editorial says it can be achieved by using a good order of calculation.
 » 7 months ago, # |   0 Need explanation for E.
 » 7 months ago, # | ← Rev. 2 →   +1 Please post test cases of this contest in DropBox.
 » 7 months ago, # |   0 Here is a problem very similar to Problem C with higher constraints, which can be solved using DP.
»
7 months ago, # |
0

# include

using namespace std;

int main() { int n;//number of people int a[n]; int t[n];//testinomy result of ith person

for(int i=0;i<n;i++{
cin>>a[i];
while(a[i]--){
int p=0;
cin>>p;
cin>>t[p];
}
}
return 0;

}

 » 7 months ago, # |   0 $E - Balanced Path$Did someone do a top down dp computing the minimum absolute difference of the sum of red numbers and the sum of blue numbers?I think it has the same time complexity as the editorial but i'm getting TLE. Code#include #define ll long long #define ld long double #define fi first #define se second #define pb push_back using namespace std; short int n,m,a[85][85],b[85][85],dp[85][85][26000],s=12800; bool use[85][85][26000]; short int fn( short int f , short int c , short int diff ){ if( f==n-1 && c==m-1) return min( abs( diff+a[f][c]-b[f][c] ) , abs( diff-a[f][c]+b[f][c] ) ); if( use[f][c][diff+s] ) return dp[f][c][diff+s]; use[f][c][diff+s] = true; short int myAns = 26000; if( f+1 < n ) myAns = min( fn( f+1 , c , diff+a[f][c]-b[f][c] ) , fn( f+1 , c , diff-a[f][c]+b[f][c] ) ); if( c+1 < m ) myAns = min( myAns , min( fn( f , c+1 , diff+a[f][c]-b[f][c] ) , fn( f , c+1 , diff-a[f][c]+b[f][c] ) ) ); return dp[f][c][diff+s] = myAns; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for(short int i=0; i> a[i][j]; for(short int i=0; i> b[i][j]; cout << fn( 0 , 0 , 0 ); return 0; } 
 » 6 months ago, # |   0 Any one please explain me Bitmask brute forcing that is used to solve C in details.