跳到正文

题解

P12006 【MX-X10-T2】[LSOT-4] 网易云

zhaoyx26约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P12006

版本

  1. v1当前

题目我就不读了。

思路

首先我们来看一下题,它输入第二行输入了 n−1n-1 个数为两两数间的和,那么我们可以来设一下,设第一个数为 tt ;

那么我们模拟一下第一个样例。

一共五个数,为 x1,x2,x3,x4,x5x1,x2,x3,x4,x5 ,设 x1x1 为t,x2x2 就为 8−t8-t,x3x3 为 6−(8−t)6-(8-t) ,也就是 6−8+t=−2+t6-8+t=-2+t,x4x4 为 7−(−2+t)=9−t7-(-2+t)=9-t,x5x5 就为 2−9+t=−7+t2-9+t=-7+t。

整理一下:

x1=tx1=t

x2=8−tx2=8-t

x3=−2+tx3=-2+t

x4=9−tx4=9-t

x5=−7+tx5=-7+t

可以发现,后面的 tt 有规律,正负正负正,所以可以发现,当 ii 为偶数时,tt 为负,反之为正,那么我们通过模拟样例还可以发现,最终我们把答案算出来,当 b×tab \times t _a 后将 mm 个全加起来,当这个答案为00时,可以算出答案,反之,算不出,就输出 ImpossibleImpossible ;

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long

int t[100010];
int c[100010];
int s[100010];
int ans;
signed main()
{
	int n,m;
	cin >> n>> m;
	c[1]=0;//初始值
    t[1]=1;
	for(int i =1; i<= n-1; i++)
	{
		cin >>s[i];
		c[i+1]=s[i]-c[i];
		t[i+1] = -1 * t[i];//相当于判断奇偶后再附上符号
	}

	int ans=0,ans2=0;
	for(int i =1; i<= m; i++)
	{
	    int a,b;
	    cin >> a>> b;
		ans=ans+b*t[a];//加起看看是否为0
		ans2=ans2+b*c[a];//计算答案
	}
	if(ans==0)
	{
		cout<<ans2;
	}
	else
	{
		cout<<"Impossible";
	}
	return 0;
}
原文