1857F Sum and Product
You have an array a a a of length n n n.
Your task is to answer q q q queries: given x , y x,y x,y, find the number of pairs i i i and j j j ( 1 ≤ i < j ≤ n 1 \le i < j \le n 1≤i<j≤n) that both a i + a j = x a_i + a_j = x ai+aj=x and a i ⋅ a j = y a_i \cdot a_j = y ai⋅aj=y.
That is, for the array [ 1 , 3 , 2 ] [1,3,2] [1,3,2] and asking for x = 3 , y = 2 x=3,y=2 x=3,y=2 the answer is 1 1 1:
- i = 1 i=1 i=1 and j = 2 j=2 j=2 fail because 1 + 3 = 4 1 + 3 = 4 1+3=4 and not 3 , 3, 3, also 1 ⋅ 3 = 3 1 \cdot 3=3 1⋅3=3 and not 2 2 2;
- i = 1 i=1 i=1 and j = 3 j=3 j=3 satisfies both conditions;
- i = 2 i=2 i=2 and j = 3 j=3 j=3 fail because 3 + 2 = 5 3 + 2 = 5 3+2=5 and not 3 , 3, 3, also 3 ⋅ 2 = 6 3 \cdot 2=6 3⋅2=6 and not 2 2 2;
换一下题面,设 a i + a j = n 1 a_i+a_j=n_1 ai+aj=n1 , a i − a j = n 2 a_i-a_j=n_2 ai−aj=n2
设一元二次方程 x 2 − n 1 ∗ x + n 2 = 0 x^2-n_1*x+n_2=0 x2−n1∗x+n2=0
根据韦达定理, x 1 + x 2 = n 1 x_1+x_2=n_1 x1+x2=n1 , x 1 ∗ x 2 = n 2 x_1*x_2=n_2 x1∗x2=n2
发现 a i a_i ai 与 a j a_j aj 就是一元二次方程的根,我们解对应根的数量即可
如果 n 1 2 − 4 ∗ n 2 < 0 n_1^2-4*n_2<0 n12−4∗n2<0 , 那么无解,也就是 n 1 2 < 4 ∗ n 2 n_1^2<4*n_2 n12<4∗n2 , 因为数据范围问题,改为判定 n 1 < 2 n 2 \sqrt{n_1}<2\sqrt{n_2} n1<2n2 , 这个式子成立就无解;如果相等(浮点数误差在 1e-6 之内)的话,就是解单根,单根 = − n 1 2 -\frac{n_1}{2} −2n1 ; 其余情况,方程有两个根,先求 Δ = n 1 2 − 4 ∗ n 2 = ( n 1 + 2 n 2 ) ( n 1 − 2 n 2 ) \Delta=n_1^2-4*n_2=(n_1+2\sqrt{n_2})(n_1-2\sqrt{n_2}) Δ=n12−4∗n2=(n1+2n2)(n1−2n2)
则 Δ = ( n 1 + 2 n 2 ) ( n 1 − 2 n 2 ) \sqrt{\Delta}=(\sqrt{n_1+2\sqrt{n_2}})(\sqrt{n_1-2\sqrt{n_2}}) Δ=(n1+2n2)(n1−2n2)
x 1 = n 1 + Δ 2 x_{1}=\frac{n_1+\sqrt{\Delta}}{2} x1=2n1+Δ
x 2 = n 1 − Δ 2 x_{2}=\frac{n_1-\sqrt{\Delta}}{2} x2=2n1−Δ
注意将数据全部转化为浮点数处理,先看看是否是接近整数,对于接近整数的, m a p map map 查询映射的数量
其实以上处理均为无效。答案只有 Δ \Delta Δ 完全平方才有效,这个优良性质使得我们不需要关心数据范围的问题
1920C Partitioning the Array
Allen has an array a 1 , a 2 , … , a n a_1, a_2,\ldots,a_n a1,a2,…,an. For every positive integer k k k that is a divisor of n n n, Allen does the following:
He partitions the array into n k \frac{n}{k} kn disjoint subarrays of length k k k. In other words, he partitions the array into the following subarrays:
[ a 1 , a 2 , … , a k ] , [ a k + 1 , a k + 2 , … , a 2 k ] , … , [ a n − k + 1 , a n − k + 2 , … , a n ] [a_1,a_2,\ldots,a_k],[a_{k+1}, a_{k+2},\ldots,a_{2k}],\ldots,[a_{n-k+1},a_{n-k+2},\ldots,a_{n}] [a1,a2,…,ak],[ak+1,ak+2,…,a2k],…,[an−k+1,an−k+2,…,an]
Allen earns one point if there exists some positive integer m m m ( m ≥ 2 m \geq 2 m≥2) such that if he replaces every element in the array with its remainder when divided by m m m, then all subarrays will be identical.
Help Allen find the number of points he will earn.
if x x x % m m m = y y y % m m m , 那么 ∣ x − y ∣ |x-y| ∣x−y∣ % m m m = 0;
枚举 k k k , 如果某个 m 成立,则对于所有 a i a_i ai ,需要满足 ∣ a i − a i + k ∣ |a_i-a_{i+k}| ∣ai−ai+k∣ % m m m = 0 0 0
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, a[200010];
void solve(){
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
int res = 0;
auto check = [&] (int k) -> void{
int g = 0;
for(int i = 1; i + k <= n; i ++){
g = __gcd(g, abs(a[i] - a[i + k]));
}
if(g != 1) res ++;
};
for(int k = 1; k <= n / k; k ++){
if(n % k == 0){
check(k);
if(k != n / k) check(n / k);
}
}
cout << res << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T --)
solve();
return 0;
}
The Walkway
There are n n n benches near the Main Walkway in Summer Infomatics School. These benches are numbered by integers from 1 1 1 to n n n in order they follow. Also there are m m m cookie sellers near the Walkway. The i i i-th ( 1 ≤ i ≤ m 1 \le i \le m 1≤i≤m) cookie sellers is located near the s i s_i si-th bench.
Petya is standing in the beginning of the Walkway. He will pass near all benches starting from the 1 1 1-st bench and ending with the n n n-th bench. Petya passes the distance between two consecutive benches in 1 1 1 minute. He has a knapsack with an infinite amount of cookies. Petya is going to eat cookies from his knapsack and buy them from cookie sellers during the walk.
Petya eats cookies only near the benches according to the following rule: he will eat the cookie near the i i i-th ( 1 ≤ i ≤ n 1 \le i \le n 1≤i≤n) bench if and only if at least one of the following conditions holds:
- There is a cookie seller near the i i i-th bench. Then Petya will buy a cookie from cookie seller and eat it immediately.
- Petya has not yet eaten a cookie. Then Petya will take a cookie from his knapsack and eat it immediately.
- At least d d d minutes passed since Petya ate the previous cookie. In other words, Petya has not eaten a cookie near the benches i − 1 , i − 2 , … , max ( i − d + 1 , 1 ) i-1, i-2, \ldots, \max(i-d+1, 1) i−1,i−2,…,max(i−d+1,1). Then Petya will take a cookie from his knapsack and eat it immediately.
You may assume that Petya eats cookies instantly. Petya will not eat two or more cookies near the same bench.
You want to minimize the number of cookies Petya will eat during his walk. In order to do this, you will ask the administration of the Summer Informatics School to remove exactly one cookie seller from the Walkway before Petya starts his walk.
Please determine the minimum possible number of cookies Petya can eat after removing exactly one cookie seller. Also determine the number of cookie sellers, such that if you remove one of them, Petya will eat the minimum possible number of cookies.
Input
The first line contains a single integer
t
t
t (
1
≤
t
≤
1
0
3
1 \le t \le 10^3
1≤t≤103) — the number of test cases.
The first line of each test case contains three integers
n
n
n,
m
m
m and
d
d
d (
2
≤
d
≤
n
≤
1
0
9
2 \le d \le n \le 10^9
2≤d≤n≤109,
2
≤
m
≤
min
(
1
0
5
,
n
)
2 \le m \le \min(10^{5}, n)
2≤m≤min(105,n)) — the number of benches, the number of cookie sellers and the value of parameter
d
d
d from the statement, respectively.
The second line of each test case contains
m
m
m integers
s
1
,
s
2
,
…
,
s
m
s_1, s_2, \ldots, s_m
s1,s2,…,sm (
1
≤
s
i
≤
n
1 \le s_i \le n
1≤si≤n) — the locations of the cookie sellers. It is guaranteed that
s
i
<
s
i
+
1
s_{i} < s_{i+1}
si<si+1 for all
1
≤
i
≤
m
−
1
1 \leq i \leq m - 1
1≤i≤m−1.
It is guaranteed that the sum of
m
m
m over all test cases does not exceed
1
0
5
10^5
105.
Output
For each test case print two integers — the minimum number of cookies that Petya can eat if exactly one cookie seller is removed, and the number of cookie sellers such that if one of them is removed, Petya will eat the minimum possible number of cookies.
从 1 走到 n,
如果当前位置有标记,代价 + 1 ;
如果 [i-d+1,i-1] 均未付出代价,i 处代价 + 1 (也就是说每 d 段必须代价 + 1)
特殊的,位置 1 一定代价 + 1
可以去掉一个标记,求答案
如果不去掉任何标记,代价 =
s[0] = 0, s[1] = 1; 原来的 [1, m] 右移到 [2, m + 1]; s[m + 2] = n + 1
如果 s[1] = 1, 就无需新增一个
for(int i = 1; i <= m + 2; i ++){
int dis = s[i] - s[i - 1] - 1;
res += dis / d;
res ++;
}
res --;
以上是不去掉任何标记的代价求法,现在是过渡到去哪个标记更划算
如果去掉中间的标记 s i s_i si , 就是去掉所有 s i s_i si 在 [1, n] 之间的标记吧
res = res - 1 - (s[i] - s[i - 1] - 1) / d - 1 - (s[i + 1] - s[i] - 1) /d + (s[i + 1] - s[i - 1] - 1) / d
Accepted
猜想是对的,但是具体处理起来还是有很多边界问题的
比如说 s 1 = 1 s_1=1 s1=1 的边界问题,如果在赛时可能还是比较难以发现的