Problem solution · Go

Codeforces 1552D — Array Differentiation

Codeforces 1552D — Array Differentiation: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
52 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 1552D — Array Differentiation, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 52 lines of Go from the credited upstream file 1552D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1552D — Array Differentiation · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") /*由于 j 和 k 没有顺序关系我们可以将 a 中的部分元素取反,记其为 a'设 b[0]=0,构造 b 为 a'[:n-1] 的前缀和若 a'[:n-1] 中存在一个子区间,其区间和等于 a'[n-1]由于 b 就是 a'[:n-1] 的前缀和,所以 a'[n-1] 也能用 b 的两个元素相减表示所以 a' 需要满足 a'[l]+...+a'[r] = a'[n-1]移项得 a'[l]+...+a'[r] - a'[n-1] = 0实际上,由于 a' 元素的顺序没有关系,这其实就是 a' 的一个子集,其元素和为 0因此得出结论:需要在 a 中找到一个子集,该子集的部分元素取反后,能够使该子集元素和为 0,若能找到这样的子集则输出 YES,否则输出 NO 进一步地,我们只需要判断 a 中是否存在两个子集和相等就行了设这两个子集为 A 和 B,则有 A[0]+A[1]+... = B[0]+B[1]+...移项得 A[0]+A[1]+... -B[0]-B[1]-... = 0注意,若两个子集有相同的元素,会在上式中消去消去后的剩余部分就是 a 的一个子集,其中部分元素取反*/ // github.com/EndlessCheng/codeforces-gofunc CF1552D(in io.Reader, out io.Writer) {	var T, n int	for Fscan(in, &T); T > 0; T-- {		Fscan(in, &n)		a := make([]int, n)		for i := range a {			Fscan(in, &a[i])		}		has := map[int]bool{}		for i := 0; i < 1<<n; i++ {			sum := 0			for j, v := range a {				sum += i >> j & 1 * v			}			has[sum] = true		}		if len(has) < 1<<n {			Fprintln(out, "YES")		} else {			Fprintln(out, "NO")		}	}} //func main() { CF1552D(os.Stdin, os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗