1364A. XXXXX
brute force, data structures, number theory, two pointers, 1200, https://codeforces.com/problemset/problem/1364/A
Ehab loves number theory, but for some reason he hates the number 𝑥. Given an array 𝑎, find the length of its longest subarray such that the sum of its elements isn't divisible by 𝑥, or determine that such subarray doesn't exist.
An array 𝑎 is a subarray of an array 𝑏 if 𝑎 can be obtained from 𝑏 by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains an integer 𝑡 (1≤𝑡≤5) — the number of test cases you need to solve. The description of the test cases follows.
The first line of each test case contains 2 integers 𝑛 and 𝑥 (1≤𝑛≤10^5^, 1≤𝑥≤10^4^) — the number of elements in the array 𝑎 and the number that Ehab hates.
The second line contains 𝑛 space-separated integers
Output
For each testcase, print the length of the longest subarray whose sum isn't divisible by 𝑥. If there's no such subarray, print −1.
Example
input
3
3 3
1 2 3
3 4
1 2 3
2 2
0 6output
2
3
-1Note
In the first test case, the subarray [2,3] has sum of elements 5, which isn't divisible by 3.
In the second test case, the sum of elements of the whole array is 6, which isn't divisible by 4.
In the third test case, all subarrays have an even sum, so the answer is −1.
数院-胡睿诚 证明:如果所有数的和不是x的倍数则不用去。现在设所有和是x倍数,如果头尾各去一段,设这两段的和分别为A, B。A和B一定至少有一个不是x倍数,那么去一头(不是x倍数的那头)就够了。
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, x = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
if s % x != 0:
print(n)
continue
l = 0
while l < n and a[l] % x == 0:
l += 1
r = n - 1
while r >= 0 and a[r] % x == 0:
r -= 1
if l == n:
print(-1)
else:
print(max(n - l - 1, r))from itertools import accumulate
def prefix_sum(nums):
# prefix = []
# total = 0
# for num in nums:
# total += num
# prefix.append(total)
# return prefix
return list(accumulate(nums))
def suffix_sum(nums):
# suffix = []
# total = 0
# # 首先将列表反转
# reversed_nums = nums[::-1]
# for num in reversed_nums:
# total += num
# suffix.append(total)
# # 将结果反转回来
# suffix.reverse()
# return suffix
return list(accumulate(reversed(nums)))[::-1]
t = int(input())
for _ in range(t):
N, x = map(int, input().split())
a = [int(i) for i in input().split()]
aprefix_sum = prefix_sum(a)
asuffix_sum = suffix_sum(a)
left = 0
right = N - 1
if right == 0:
if a[0] % x != 0:
print(1)
else:
print(-1)
continue
leftmax = 0
rightmax = 0
while left != right:
total = asuffix_sum[left]
if total % x != 0:
leftmax = right - left + 1
break
else:
left += 1
left = 0
right = N - 1
while left != right:
total = aprefix_sum[right]
if total % x != 0:
rightmax = right - left + 1
break
else:
right -= 1
if leftmax == 0 and rightmax == 0:
print(-1)
else:
print(max(leftmax, rightmax))for _ in range(int(input())):
a, b = map(int, input().split())
s = -1
A = list(map(lambda x: int(x) % b, input().split()))
if sum(A) % b:
print(a)
continue
for i in range(a//2+1):
if A[i] or A[~i]:
s = a-i-1
break
print(s)Pypy3 可以AC。使用tree segment,时间复杂度是O(n*logn)
# CF 1364A
# def prefix_sum(nums):
# prefix = []
# total = 0
# for num in nums:
# total += num
# prefix.append(total)
# return prefix
# def suffix_sum(nums):
# suffix = []
# total = 0
# # 首先将列表反转
# reversed_nums = nums[::-1]
# for num in reversed_nums:
# total += num
# suffix.append(total)
# # 将结果反转回来
# suffix.reverse()
# return suffix
t = int(input())
ans = []
for _ in range(t):
n, x = map(int, input().split())
a = [int(i) for i in input().split()]
# Segment tree | Efficient implementation
# https://www.geeksforgeeks.org/segment-tree-efficient-implementation/
# Max size of tree
tree = [0] * (2 * n);
def build(arr) :
# insert leaf nodes in tree
for i in range(n) :
tree[n + i] = arr[i];
# build the tree by calculating parents
for i in range(n - 1, 0, -1) :
tree[i] = tree[i << 1] + tree[i << 1 | 1];
# function to update a tree node
def updateTreeNode(p, value) :
# set value at position p
tree[p + n] = value;
p = p + n;
# move upward and update parents
i = p;
while i > 1 :
tree[i >> 1] = tree[i] + tree[i ^ 1];
i >>= 1;
# function to get sum on interval [l, r)
def query(l, r) :
res = 0;
# loop to find the sum in the range
l += n;
r += n;
while l < r :
if (l & 1) :
res += tree[l];
l += 1
if (r & 1) :
r -= 1;
res += tree[r];
l >>= 1;
r >>= 1
return res;
#aprefix_sum = prefix_sum(a)
#asuffix_sum = suffix_sum(a)
build([i%x for i in a]);
left = 0
right = n - 1
if right == 0:
if a[0] % x !=0:
print(1)
else:
print(-1)
continue
leftmax = 0
rightmax = 0
while left != right:
#total = asuffix_sum[left]
total = query(left, right+1)
if total % x != 0:
leftmax = right - left + 1
break
else:
left += 1
left = 0
right = n - 1
while left != right:
#total = aprefix_sum[right]
total = query(left, right+1)
if total % x != 0:
rightmax = right - left + 1
break
else:
right -= 1
if leftmax == 0 and rightmax == 0:
#print(-1)
ans.append(-1)
else:
#print(max(leftmax, rightmax))
ans.append(max(leftmax, rightmax))
print('\n'.join(map(str,ans)))如果用sum求和,O(n^2),pypy3也会在test3 超时。