import java.util.*; public class Main { stati...
Créé le : 13 juillet 2025
Répondu en utilisant O4 Mini High par Chat01
Créé le : 13 juillet 2025
Répondu en utilisant O4 Mini High par Chat01
import java.util.*;
public class Main {
static final int MOD = (int)1e9+7;
textstatic class Mat { long a00, a01, a10, a11; Mat(long a00, long a01, long a10, long a11) { this.a00 = a00; this.a01 = a01; this.a10 = a10; this.a11 = a11; } Mat mul(Mat o) { return new Mat( (a00 * o.a00 + a01 * o.a10) % MOD, (a00 * o.a01 + a01 * o.a11) % MOD, (a10 * o.a00 + a11 * o.a10) % MOD, (a10 * o.a01 + a11 * o.a11) % MOD ); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int Q = sc.nextInt(); Mat[] base = new Mat[Math.max(0, N-1)]; for (int i = 0; i < N; i++) { long r = sc.nextLong(); long b = sc.nextLong(); long w = sc.nextLong(); if (i < N-1) { base[i] = new Mat(r % MOD, w % MOD, w % MOD, b % MOD); } } long xorSum = 0; while (Q-- > 0) { int type = sc.nextInt(); if (type == 0) { int u = sc.nextInt(); int v = sc.nextInt(); int c = sc.nextInt(); if (u > v) { int tmp = u; u = v; v = tmp; } Mat cur = new Mat(1, 0, 0, 1); for (int k = u; k < v; k++) { cur = cur.mul(base[k-1]); } long ways = (c == 0 ? cur.a00 : cur.a11); xorSum^ = ways; } else { int i = type; long r = sc.nextLong(); long b = sc.nextLong(); long w = sc.nextLong(); if (i >= 1 && i <= N-1) { base[i-1] = new Mat(r % MOD, w % MOD, w % MOD, b % MOD); } } } System.out.println(xorSum); sc.close(); }
}
this code is passing 8/12 tc but giving wrong answer in 4 tc
The core issue isn’t modular arithmetic or overflow—it’s in how you’re reading the input:
You’re reading one too many “edge”-triples.
The problem statement gives you exactly N−1 triples (r, b, w), one per edge, but your loop runs i=0..N−1
, so you actually consume the first query’s parameters as that last “useless” triple.
You never read the update index.
On an update you do:
javaint type = sc.nextInt(); // … } else { int i = type; // WRONG: `type` is just the code (1), not the edge-index! long r = sc.nextLong(); long b = sc.nextLong(); long w = sc.nextLong(); base[i-1] = …; }
But the input for an update is actually 1 idx r b w
, so you need to do:
javaint code = sc.nextInt(); if (code == 0) { … query … } else { int idx = sc.nextInt(); // <-- read the edge-index long r = sc.nextLong(); long b = sc.nextLong(); long w = sc.nextLong(); base[idx-1] = new Mat(r%MOD, w%MOD, w%MOD, b%MOD); }
With those two fixes your input will line up correctly and you won’t silently “eat” part of the queries. Here’s a drop-in corrected version of your main
:
javaimport java.util.*; public class Main { static final int MOD = (int)1e9+7; static class Mat { long a00, a01, a10, a11; Mat(long a00, long a01, long a10, long a11) { this.a00 = a00; this.a01 = a01; this.a10 = a10; this.a11 = a11; } Mat mul(Mat o) { return new Mat( (a00*o.a00 + a01*o.a10) % MOD, (a00*o.a01 + a01*o.a11) % MOD, (a10*o.a00 + a11*o.a10) % MOD, (a10*o.a01 + a11*o.a11) % MOD ); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int Q = sc.nextInt(); // read exactly N-1 triples Mat[] base = new Mat[N-1]; for (int i = 0; i < N-1; i++) { long r = sc.nextLong(); long b = sc.nextLong(); long w = sc.nextLong(); base[i] = new Mat(r % MOD, w % MOD, w % MOD, b % MOD); } long xorSum = 0; while (Q-- > 0) { int type = sc.nextInt(); if (type == 0) { // a range‐query int u = sc.nextInt(), v = sc.nextInt(), c = sc.nextInt(); if (u > v) { int t=u; u=v; v=t; } Mat cur = new Mat(1,0,0,1); for (int k = u; k < v; k++) { cur = cur.mul(base[k-1]); } long ways = (c == 0 ? cur.a00 : cur.a11); xorSum ^= ways; } else { // an update: read the actual index int idx = sc.nextInt(); // <-- fix! long r = sc.nextLong(); long b = sc.nextLong(); long w = sc.nextLong(); if (idx >= 1 && idx < N) { base[idx-1] = new Mat(r % MOD, w % MOD, w % MOD, b % MOD); } } } System.out.println(xorSum); sc.close(); } }
With these changes:
idx
(instead of mis-using type
).That should eliminate the “mysterious” misalignment that was wrecking 4 of your 12 tests.