A:简单dp
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
ll d[1000000][2];
ll n;
int main() {
d[1][0]=1;//c
d[1][1]=1;//y
cin>>n;
for(int i=2;i<=n;i++){
(d[i][0]+=d[i-1][1])%=mod;
(d[i][1]+=d[i-1][1])%=mod;
(d[i][1]+=d[i-1][0])%=mod;
}
cout<<((d[n][1]+d[n][0])%mod);
return 0;
}
B:分块
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e6 + 10;
const ll mod = 1e9 + 7;
ll n, d[N], ans;
int main() {
scanf("%lld", &n);
for(int i = 1; i <= n; i++)
d[i] = i;
for(ll j = 1; j <= n; j++) {
for(int i = j; i <= n; i += j) {
ll k = min(n, i + j - 1);
ans = (ans + (((i + k) * (k - i + 1) >> 1) % mod) * d[i / j]) % mod;
d[i / j] = (d[i / j] * (i / j) % mod);
}
}
printf("%lld\n", ans);
return 0;
}
C:富豪凯匹配串
如果暴力求的话复杂度是100030001000,3e9铁定T,所以我们可以用bitset来优化,bitset是stl的黑科技,可以让你复杂度除32,所以用了后复杂度9e7,还好稳过。
判q次时,对读入的每个字符串当作二进制处理,下划线默认为0,然后存进x,x先取反,然后对y,当当前为下划线时,是1,所以a[i]^=x,a[i]|=y,如果全是1,则ans++。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e3 + 10;
bitset<N> b[N], a[N];
char c[N];
int n, m, q;
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
scanf("%s", c);
for(int j = 0; j < m; j++)
b[i][j] = (c[j] - '0');
}
scanf("%d", &q);
while(q--) {
int ans = 0;
scanf("%s", c);
bitset<N> x, y;
for(int i = 0; i < m; i++) {
if(c[i] != '_')
x[i] = (c[i] - '0');
else
x[i] = 0, y[i] = 1;
}
x = (~x);
for(int i = 1; i <= n; i++) {
a[i] = b[i] ^ x;
a[i] |= y;
if(a[i].all())
ans++;
}
printf("%d\n", ans);
}
return 0;
}