There are buildings along the AtCoder Street, numbered through from west to east. Initially, Buildings have the heights of , respectively.
Takahashi, the president of ARC Wrecker, Inc., plans to choose integers and and make the heights of Buildings all zero. To do so, he can use the following two kinds of operations any number of times in any order:
Note that the range of depends on .
How many choices of are there where Takahashi can realize his plan?
Input is given from Standard Input in the following format:
Print the answer.
5 5 8 8 6 6
3
Takahashi can realize his plan for .
For example, for , the following sequence of operations make the heights of Buildings all zero.
For the remaining seven choices of , there is no sequence of operations that can realize his plan.
7 12 8 11 3 3 13 2
3
Takahashi can realize his plan for .
For example, for , the following figure shows one possible solution.
10 8 6 3 9 5 4 7 2 1 10
1
Takahashi can realize his plan for only.
14 630551244 683685976 249199599 863395255 667330388 617766025 564631293 614195656 944865979 277535591 390222868 527065404 136842536 971731491
8
题意:
有 n 个数,每次可以将 a[x],a[x+1] 同时 +1/-1 ,问存在多少区间同时减为 0
题解:
直觉告诉我用差分来做,然后模拟出来的结果时间复杂度都是 O(n^2)
直到看到了 hu_tao 大佬的讨论,一语惊醒,每次操作一定是将一个奇数位和偶数位同时操作,所以无论怎么变一个区间想要同时变为 0,那么这个区间上奇数位置和偶数位置的加和是相同的;
所以将奇数位置处的值置为负数,利用同余定理,就可以找到任意一个连续的子段加和为 0
const int N=1e6+5;
int n, m, _;
int i, j, k;
ll a[N];
map<ll,int> mp;
signed main()
{
//IOS;
while(~sd(n)){
for(int i=1;i<=n;i++){
sll(a[i]);
if(i%2==0) a[i]=-a[i];
}
ll ans=0;
mp[0]++;
for(int i=1;i<=n;i++){
a[i]+=a[i-1];
if(mp[a[i]]) ans+=mp[a[i]];
mp[a[i]]++;
}
pll(ans);
mp.clear();
}
//PAUSE;
return 0;
}
AtCoder Regular Contest 119 C - ARC Wrecker 2(同余定理+思维)
原文:https://www.cnblogs.com/Segment-Tree/p/14817521.html