2021牛客寒假算法基础集训营4
B 武辰延的字符串
exgcd
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
typedef long long LL;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int N = 1e7 + 10000;
char sa[1100000], sb[1100000];
int lena, lenb;
int p[1100000], ex[1100000];
//p数组是用来让B串自己匹配自己的
void exkmp() {
p[1] = lenb;
int x = 1;
while (sb[x] == sb[x + 1] && x + 1 <= lenb) x++;//因为我们p[1]是具有一定性,所以我们不能直接用,所以要先暴力求出p[2]
p[2] = x - 1;
int k = 2;
for (int i = 3; i <= lenb; i++) {
int pp = k + p[k] - 1, L = p[i - k + 1];//pp实际上是p
if (i + L < pp + 1) p[i] = L;//i-k+L<pp-k+1化简后i+L<pp
else {
int j = pp - i + 1;
if (j < 0) j = 0;
while (sb[j + 1] == sb[i + j] && i + j <= lenb) j++;
p[i] = j;
k = i;
}
}
x = 1;
while (sa[x] == sb[x] && x <= lenb) x++;//ex[1]并不具有一定性,所以我们暴力求出ex[1]
ex[1] = x - 1;
k = 1;
for (int i = 2; i <= lena; i++) {
int pp = k + ex[k] - 1, L = p[i - k + 1];
if (i + L < pp + 1) ex[i] = L;
else {
int j = pp - i + 1;
if (j < 0) j = 0;
while (sb[j + 1] == sa[i + j] && i + j <= lena && j <= lenb) j++;
ex[i] = j;
k = i;
}
}
}
void solve() {
cin>>sb+1>>sa+1;
lena = strlen(sa + 1);
lenb = strlen(sb + 1);
exkmp();
int sum = 0;
for (int i = 1; i <=ex[1]; i++) sum+=ex[i+1];
cout << sum << "\n";
}
signed main() {
// ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}
F 魏迟燕的自走棋
并查集
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int N = 1e7 + 10000;
int fa[100010],flag[100010];
struct no{
int x,y,v;
}w[100010];
bool cmp(no a,no b){
return a.v>b.v;
}
int f(int x){
return fa[x]==x? x:fa[x]=f(fa[x]);
}
void solve() {
int n,m;
cin>>n>>m;
for (int i = 1; i <=n; ++i) {
fa[i]=i,flag[i]=0;
}
for (int i = 1; i <=m; ++i) {
int temp;
cin>>temp;
if(temp==1){
cin>>temp>>w[i].v;
w[i].x =temp,w[i].y=temp;
}
else
cin>>w[i].x >>w[i].y >>w[i].v ;
}
sort(w+1,w+m+1,cmp);
int ans=0;
for (int i = 1; i <=m; ++i) {
int x=f(w[i].x),y=f(w[i].y);
if(x!=y&&(flag[x]==0||flag[y]==0)){
ans+=w[i].v;
fa[y]=x;
flag[x]=(flag[x]||flag[y]);
}
else if(flag[x]==0){
ans+=w[i].v;
flag[f(x)]=1;
}
}
cout<<ans;
}
signed main() {
// ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}
J 邬澄瑶的公约数
因式分解
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
typedef long long LL;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 1e7 + 10000;
int fastpow(int x,int y){
int res=1;
while (y){
if (y&1)res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res;
}
int x[maxn], p[maxn];
map<int, int> xx;
map<int, int> pp;
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
for (int i = 0; i < n; ++i) {
cin>>p[i];
}
for (int j = 0; j < n; ++j) {
int num = x[j];
for (int i = 2; i <= num; i++) {
int f=0;
while (num % i == 0) {
num = num / i;
f++;
}
if (pp[i]>0) pp[i]=min(f*p[j],pp[i]);
else pp[i]=f*p[j];
if (f) xx[i]++;
}
}
int sum=1;
for(auto u:xx){
if (u.second==n)
sum=fastpow(u.first,pp[u.first])*sum%mod;
}
cout<<sum%mod;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}