A - R does not simulate all the code and explain the focus of the last two questions
Lele small string S
https://ac.nowcoder.com/acm/contest/1877/S
C the number of records to traverse from front to back, showing there are several characters before the current character C. From the back through the records of the number N, N represents the number of characters after the character in the current
Finally, from front to back through the array, when the current character is H, there is numC * numN case, is the answer to every summed.
#include<bits/stdc++.h>
using namespace std;
const int MAXN=8000;
int num2[MAXN+11];
int main() {
string s;
cin>>s;
for(int i=s.length()-1;i>=0;i--) {
num2[i]=num2[i+1];
if(s[i]=='N')
num2[i]++;
}
long long cnt=0;
for(int i=0;i<s.length();i++) {
if(s[i]=='C') {
for(int j=i+1;j<s.length();j++) {
if(s[j]=='H')
cnt+=num2[j];
}
}
}
printf("%lld\n",cnt);
return 0;
}
Lele small number of T Sec
https://ac.nowcoder.com/acm/contest/1877/T
Special record it, Portal
The first time I saw such a solution, number theory only GCD. %%%
#include<cstdio>
#include<cstring>
#include <algorithm>
using namespace std;
int ones[9999],tens[999], n, i, j, k, s, t, api, apj, aps, apt;
bool ck() {
int p, r;
if (i > 5)
return 1;
p = s;
r = t;
for (int q = 0; q < j; q++)
p = p * 10 + s;
for (int q = 0; q < i - j; q++)
p = p * 10;
for (int q = 1; q < i - j; q++)
r = r * 10 + t;
return p + r > n;
}
int main() {
while (scanf("%d",&n), n) {
printf("%d: ", n);
if (n == 1) {
puts("10");
continue;
}
ones[0] = 1;
tens[0] = 1;
for (i = 1; i < 9999; i++)
ones[i] = (ones[i - 1] * 10 + 1) % n;
for (i = 1; i < 999; i++)
tens[i] = tens[i - 1] * 10 % n;
for (i = 1, aps = 0; i < 9999; i++) {
k = 0;
if ((n % 10 == 0 || n % 25 == 0) && i > 11)
k = i - 11;
for (j = k; j < i; j++)
for (s = 1; s < 10; s++)
for (t = 0; t < (n % 10 ? 10 : 1); t++)
if (t != s && (((long long)ones[j]) * tens[i - j] * s + ones[i - j - 1] * t) % n == 0 && ck() &&
(!aps || s < aps || s == aps && j > apj && s < apt)) {
api = i;
apj = j;
aps = s;
apt = t;
}
if (aps)
break;
}
for (int x = 0; x < apj + 1; x++)
printf("%d", aps);
for (int x = 0; x < api - apj; x++)
printf("%d", apt);
printf("\n");
}
return 0;
}