2 条题解
-
-3
#define int long long const int MAXN = 2e5 + 10, mod = 1e9 + 7; inline int power(int x, int k) { int ret = 1; while(k) { if(k & 1) ret = ret * x % mod; x = x * x % mod; k >>= 1; } return ret; } inline int inv(int x) {return power(x, mod - 2);} int T, n; int x[MAXN], y[MAXN], z[MAXN]; int a[MAXN], b[MAXN], c[MAXN]; int s[MAXN]; signed main() { T = read(); while(T--) { n = read(); for(int i = 1; i <= n; i++) { x[i] = read(); y[i] = read(); z[i] = read(); } //对每个x,y,z,x[i]s[i] - y[i]s[i-1] + y[i]s[n] = z[i] for(int i = 1; i < n; i++) {//高斯消元解前缀和 a[i] = mod - y[i]; b[i] = x[i]; c[i] = y[i]; } a[n] = mod - y[n]; c[n] = (x[n] + y[n]) % mod; for(int i = 1; i < n; i++) {//用第i行消掉第i+1行的第i列 int k = a[i + 1] * inv(b[i]) % mod; (a[i + 1] -= k * b[i] % mod) %= mod; (c[i + 1] -= k * c[i] % mod) %= mod; (z[i + 1] -= k * z[i] % mod) %= mod; } int roots = 1; if(c[n] == 0 && z[n] != 0) { putchar('0'); putchar('\n'); continue; } else if(c[n] == 0 && z[n] == 0) roots = mod; else s[n] = z[n] * inv(c[n]) % mod; for(int i = 1; i <= n - 1; i++) { int right = (z[i] - c[i] * s[n] % mod) % mod; if(b[i] == 0 && right != 0) {roots = 0; break;} else if(b[i] == 0 && right == 0) roots += mod; else s[i] = ((z[i] - c[i] * s[n] % mod) * inv(b[i]) % mod + mod) % mod; } if(roots != 1) {printf("%lld\n", roots); continue;} else {putchar('1'); putchar('\n');} for(int i = 1; i <= n; i++) printf("%lld ", ((s[i] - s[i - 1]) % mod + mod) % mod); putchar('\n'); } return 0; }
信息
- ID
- 208
- 时间
- 2000ms
- 内存
- 512MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 258
- 已通过
- 17
- 上传者