C - Sigma Problem
Problem Statement
For positive integers 𝑥x and 𝑦y, define 𝑓(𝑥,𝑦)f(x,y) as the remainder of (𝑥+𝑦)(x+y) divided by 108108.
You are given a sequence of positive integers 𝐴=(𝐴1,…,𝐴𝑁)A=(A1,…,AN) of length 𝑁N. Find the value of the following expression:
∑𝑖=1𝑁−1∑𝑗=𝑖+1𝑁𝑓(𝐴𝑖,𝐴𝑗)i=1∑N−1j=i+1∑Nf(Ai,Aj).
Constraints
- 2≤𝑁≤3×10^5 2≤N≤3×10^5
- 1≤𝐴𝑖<1081≤Ai<108
- All input values are integers.
Sample Input 1
3 3 50000001 50000002
Sample Output 1
100000012
题意:数组里的数两两相加对1e8取模后求和。
数组大小3e5暴力肯定超时。每个数都会加n-1次,所以先将这些数加起来,然后减掉需要取模的那些和的1e8就行。所以先排序方便使用lowerbound判断有几个和大于1e8,减去相应数量的1e8就行。
(因为1e8不是longlong wa了……)
总而言之看代码。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
const double pi = 3.1415926;
const int N = 1000010, INF = 1e8;
LL n, m;
LL a[N];
int main() {
cin >> n;
LL ans = 0, t;
for(int i = 0;i < n;i ++) cin >> a[i], ans += a[i] * (n - 1);
sort(a, a + n);
for(LL i = 0;i < n;i ++) {
t = lower_bound(a, a + n, 100000000 - a[i]) - a;
t = max(t, i + 1);
if(t > i) ans -= (n - t) * (1e8);
}
cout << ans << '\n';
return 0;
}
E - Yet Another Sigma Problem
Problem Statement
For strings 𝑥x and 𝑦y, define 𝑓(𝑥,𝑦)f(x,y) as follows:
- 𝑓(𝑥,𝑦)f(x,y) is the length of the longest common prefix of 𝑥x and 𝑦y.
You are given 𝑁N strings (𝑆1,…,𝑆𝑁)(S1,…,SN) consisting of lowercase English letters. Find the value of the following expression:
∑𝑖=1𝑁−1∑𝑗=𝑖+1𝑁𝑓(𝑆𝑖,𝑆𝑗)i=1∑N−1j=i+1∑Nf(Si,Sj).
Constraints
- 2≤𝑁≤3×1052≤N≤3×105
- 𝑆𝑖Si is a string consisting of lowercase English letters.
- 1≤∣𝑆𝑖∣1≤∣Si∣
- ∣𝑆1∣+∣𝑆2∣+…+∣𝑆𝑁∣≤3×105∣S1∣+∣S2∣+…+∣SN∣≤3×105
- All input numbers are integers.
Input
The input is given from Standard Input in the following format:
Sample Input 1
3 ab abc arc
Sample Output 1
4
题意:给一些小写字母组成的字符串,求他们每两个之间的公共前缀的长度之和。
字典树
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
const double pi = 3.1415926;
const int N = 500010, INF = 998244353;
int n, m;
int tr[N][26], cnt[N], idx;
LL ans = 0;
void add(string s) {
int p = 0;
for(auto t : s) {
int u = t - 'a';
if(!tr[p][u]) tr[p][u] = ++ idx;
p = tr[p][u];
ans += cnt[p];
cnt[p] ++;
}
}
int main() {
cin >> n;
for(int i = 0;i < n;i ++) {
string s;
cin >> s;
add(s);
}
cout << ans << '\n';
return 0;
}
595






