钙 模拟赛总结与代码
10.28上午QBXT
比较简单的一套模拟赛吧。
T1:随便做
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 1e5 + 7;
char s[maxN];
int main() {
freopen("increase.in","r",stdin);
freopen("increase.out","w",stdout);
scanf("%s",s + 1);
int n = strlen(s + 1);
int now = s[1] - '0';
int pos = 1,tmp = 1;
for(int i = 2;i <= n;++ i) {
int x = s[i] - '0';
if(x > now) {
now = x;
pos = i;
}else {
if(x < now) break;
}
if(x == now) tmp = i;
}
if(tmp == n) {
printf("%s",s + 1);
return 0;
}
bool flag = false;
for(int i = 1;i < pos;++ i) {
printf("%c",s[i]);
}
if(s[pos] - '0' - 1 != 0)
printf("%c",s[pos] - 1);
for(int i = pos + 1;i <= n;++ i) {
printf("9");
}
return 0;
}
T2:随便做
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define int long long
const int mod = 1e12 + 7;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 4e4 + 7;
int a[maxN] , ans,p[100007],b[maxN],s[maxN];
int query(int x) {
int ans = 0;
for(;x;x -= x & -x) ans = (ans + p[x]) % mod;
return ans;
}
const int MAX = 1e5;
void add(int x , int val) {
for(;x <= MAX;x += x & -x) {
p[x] = (p[x] + val) % mod;
}
return ;
}
int mul(int a , int b) {
int ans = 0;
for(int now = a;b;b >>= 1,now = (now + now) % mod) {
if(b & 1) ans = (ans + now) % mod;
}
return ans;
}
signed main() {
freopen("multiplication.in","r",stdin);
freopen("multiplication.out","w",stdout);
int n = gi();
for(int i = 1;i <= n;++ i) s[i] = b[i] = gi();
sort(s + 1,s + n + 1);
for(int i = 1;i <= n;++ i) a[i] = lower_bound(s + 1,s + n + 1,b[i]) - s;
for(int i = 1;i <= n;++ i) {
int tmp = query(n) - query(a[i]);
tmp %= mod;
tmp += mod;
tmp %= mod;
ans = ans + mul(mul(tmp , n - i + 1) , b[i]);
ans %= mod;
add(a[i] , b[i] * i % mod);
}
ans %= mod;
ans += mod;
ans %= mod;
cout << ans;
return 0;
}
T3:简单题
nmd,我不会扫描线。
会了扫描线这是一个简单题吧
不过这里没有必要用线段树维护扫描线。
考虑出现
10.27ZR
T1
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
const int mod = 987654321;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 1000 + 7;
int f[maxN], sum[maxN], a[maxN];
bool vis[1000007];
int prime[100007];
int num;
void Pre() {
int n = 1000000;
vis[1] = 1;
for(int i = 2;i <= n;++ i) {
if(!vis[i]) prime[++ num] = i;
for(int j = 1;j <= num && i * prime[j] <= n;++ j) {
vis[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
return;
}
int main() {
Pre();
int n = gi();
for(int i = 1;i <= n;++ i) {
a[i] = gi();
sum[i] = sum[i - 1] + a[i];
}
f[0] = 1;
for(int i = 1;i <= n;++ i) {
for(int j = 0;j < i;++ j) {
if(!vis[sum[i] - sum[j]]) f[i] = (f[i] + f[j]) % mod;
}
}
cout << f[n];
return 0;
}
T2:
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100000 + 7;
const int inf = 0x7fffffff;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
int n, m;
int fa[maxN];
struct Edge{
int u , v, w;
}a[maxN];
bool cmp(Edge a , Edge b) {
return a.w > b.w;
}
int get(int x) {return fa[x] == x ? x : fa[x] = get(fa[x]);}
int merge(int x , int y) {fa[get(x)] = get(y);}
struct Node {
int v , nex, w;
}Map[maxN];
int num , head[maxN];
void add_Node(int u , int v, int w) {
Map[++ num] = (Node) {v , head[u], w};
head[u] = num;
}
void Work_one() {
sort(a + 1, a + m + 1, cmp);
int cnt = 0;
for(int i = 1;i <= m;++ i) {
int fax = get(a[i].u) , fay = get(a[i].v);
if(fax != fay) {
merge(a[i].u , a[i].v);
add_Node(a[i].u , a[i].v, a[i].w);add_Node(a[i].v , a[i].u, a[i].w);
cnt ++;
if(cnt == n - 1) break;
}
}
return;
}
int dep[maxN];
int f[maxN][23], v[maxN][23];
bool vis[maxN];
void dfs(int now , int fa, int w) {
dep[now] = dep[fa] + 1;
vis[now] = true;
f[now][0] = fa;
v[now][0] = w;
for(int i = 1;i <= 20;++ i) {
f[now][i] = f[f[now][i - 1]][i - 1];
v[now][i] = min(v[now][i - 1] , v[f[now][i - 1]][i - 1]);
}
for(int i = head[now];i;i = Map[i].nex) {
int v = Map[i].v;
if(v == fa) continue;
dfs(v , now, Map[i].w);
}
return;
}
void Work_two() {
for(int i = 1;i <= n;++ i) {
if(!vis[i]) {
dfs(i , 0, -1);
}
}
}
int lca(int x , int y) {
int ans = inf;
if(dep[x] > dep[y]) swap(x , y);
for(int i = 20;i >= 0;-- i) {
if(dep[y] - (1 << i) >= dep[x]) {ans = min(ans , v[y][i]);y = f[y][i]; }
}
if(x == y) return ans;
for(int i = 20;i >= 0;-- i) {
if(f[y][i] != f[x][i]) {
ans = min(ans , v[y][i]);
ans = min(ans , v[x][i]);
y = f[y][i];
x = f[x][i];
}
}
ans = min(ans, min(v[x][0] , v[y][0]));
return ans;
}
int main() {
// freopen("testdata.in","r",stdin);
n = gi(), m = gi(); int q = gi();
rep(i , 1, n) fa[i] = i;
for(int i = 1;i <= m;++ i) {a[i].u = gi();a[i].v = gi();a[i].w = gi();}
Work_one();
Work_two();
for(int i = 1;i <= q;++ i) {
int x = gi(), y = gi(), w = gi();
int ans = lca(x , y);
if(ans == -1) puts("No");
else {
if(ans >= w) {
puts("Yes");
}else puts("No");
}
// printf("%d\n",ans);
}
return 0;
}
T3
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define pz putchar('0');
#include <map>
using namespace std;
const int MO = 15;
struct Big {
int len, data[10001];
void clear() {
memset(this, 0, sizeof(*this));
}
int & operator [] (int k) {
return data[k];
}
Big & operator = (int k) {
clear();
len = 0;
while(k) {
++len;
data[len] = k & MO;
k >>= 4;
}
if (len == 0) ++len;
return *this;
}
Big operator * (Big & A) {
Big temp;
temp.clear();
temp.len = len + A.len - 1;
for (int i = 1; i <= len; i++)
for (int j = 1; j <= A.len; j++) {
temp[i + j - 1] += A[j] * data[i];
temp[i + j] += (temp[i + j - 1] >> 4);
temp[i + j - 1] &= MO;
}
while(temp[temp.len + 1]) ++temp.len;
return temp;
}
void print() {
for (int i = len; i >= 1; i--) printf("%X", data[i]);
putchar('\n');
}
} temp, ans;
map<int, bool> M;
bool f[1000001];
int pnum, p[100001];
void GETP(int M) {
memset(f, 1, sizeof(f));
f[0] = f[1] = false;
p[pnum = 1] = 2;
for (int now = 2; now < M;) {
for (int j = now + now; j <= M; j += now) f[j] = false;
++now;
while(now < M && !f[now]) ++now;
if (f[now]) p[++pnum] = now;
}
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
void work(int num) {
for (int i = 1; i <= pnum; i++) {
if (num % p[i] == 0)
if (M[p[i]] == 0) {
M[p[i]] = true;
temp = p[i];
ans = ans * temp;
}
while(num % p[i] == 0) num /= p[i];
}
if (num != 1)
if (M[num] == 0) {
M[num] = true;
temp = num;
ans = ans * temp;
}
}
int main() {
ans = 1;
int t;
scanf("%d", &t);
GETP(100000);
while(t--) {
int a, b;
scanf("%d%d", &a, &b);
int d = gcd(a, b);
a /= d;
b /= d;
work(b);
}
ans.print();
}
10.24
T3不会做,T1、T2傻逼题
T1:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
const int maxN = 1e7 + 7;
const int mod = 1e9 + 7;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
int f[2][2];
char s[maxN];
int main() {
// freopen("zero.in","r",stdin);
// freopen("zero.out","w",stdout);
int n = gi();
scanf("%s",s + 1);
f[0][0] = 1;
f[0][1] = 1;
for(int i = 1;i <= n;++ i) {
int now = i & 1;
int last = now ^ 1;
if(s[i] == '&') {
f[now][0] = (1ll * f[last][0] * 2 + f[last][1]) % mod;
f[now][1] = f[last][1];
}
if(s[i] == '|') {
f[now][0] = f[last][0];
f[now][1] = (1ll * f[last][1] * 2 + f[last][0]) % mod;
}
if(s[i] == '^') {
f[now][0] = (f[last][1] + f[last][0]) % mod;
f[now][1] = (f[last][0] + f[last][1]) % mod;
}
}
printf("%d", f[n & 1][1]);
return 0;
}
T2:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define int long long
const int mod = 1e9 + 7;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
int gcd(int a , int b) {
return !b ? a : gcd(b , a % b);
}
int L, R, K;
void Sub1() {
int ans = 0;
for(int a = L;a <= R - 2 * K;++ a) {
if(gcd(a , 2 * K) == 1) {
ans ++;
}
}
printf("%d\n",ans);
}
vector<int> q;
int n;
void work(int x) {
int p = sqrt(x);
for(int i = 2;i <= p;++ i) {
if(x % i == 0) {
q.push_back(i);
++ n;
while(x % i == 0) x /= i;
}
}
if(x != 1) {
q.push_back(x);
++ n;
}
return;
}
int sum;
bool vis[312];
int TQL;
void dfs(int now , int tot, int pi) {
if(now == n + 1) {
if(tot & 1) sum -= TQL / pi;
else sum += TQL / pi;
return;
}
dfs(now + 1,tot + 1, pi * q[now - 1]);
dfs(now + 1,tot, pi);
}
int solve() {
sum = 0;
dfs(1 , 0, 1LL);
return sum;
}
signed main() {
// freopen("prime.in","r",stdin);
// freopen("prime.out","w",stdout);
L = gi(), R = gi(),K = gi();
if(R - L <= 1000000) return Sub1(),0;
work(2 * K);
TQL = R - 2 * K;
int ans = solve();
TQL = L - 1;
ans -= solve();
cout << ans % mod;
return 0;
}
10.23
懒得讲,都挺简单的。
T1:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define int long long
const int mod = 998244353;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
int Gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = (1ll * x * 10 % (mod - 1) + c - '0') % (mod - 1);c = gc;}
return x * f;
}
const int maxN = 1e6 + 7;
char s[maxN];
int fast_pow(int a, int b) {
int ans = 1;
for(int now = a;b;b >>= 1,now = now * now % mod) {
if(b & 1) ans = ans * now % mod;
}
return ans;
}
int inv(int x) {
return fast_pow(x , mod - 2);
}
int n;
signed main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
// cin >> n;
int n = Gi();
scanf("%s",s + 1);
int l = strlen(s + 1);
if(l > n) return cout << fast_pow(26ll,n)<<"\n",0;
else if(l == n) return cout << fast_pow(26ll,n) - 1,0;
int ans = fast_pow(26ll,n);
ans = ans - (25ll * l % mod) * fast_pow(26ll , n) % mod * inv(fast_pow(26ll,l + 1)) % mod;
ans = ans - fast_pow(26,n - l) ;
ans %= mod;
ans += mod;
ans %= mod;
cout << ans;
return 0;
}
/*
10000000000 aaa
*/
T2:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define lson now << 1
#define rson now << 1 | 1
#define int long long
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 1e5 + 7;
struct Node {
int x , y;
}a[maxN];
int f[maxN];
int b[maxN << 1];
PII p[maxN];
struct Tree {
int l , r;
int maxx;
}tree1[maxN << 3] , tree2[maxN << 3];
void updata(int now , int id) {
if(id == 1) tree1[now].maxx = max(tree1[lson].maxx , tree1[rson].maxx);
else tree2[now].maxx = max(tree2[lson].maxx , tree2[rson].maxx);
}
void build(int now , int l, int r) {
tree1[now].l = l;tree1[now].r = r;
tree2[now].l = l;tree2[now].r = r;
if(l == r) return;
int mid = (l + r) >> 1;
build(lson , l, mid);
build(rson , mid + 1, r);
}
void change(int now , int id, int pos, int val) {
if(tree1[now].l == tree1[now].r) {
if(id == 1) tree1[now].maxx = max(tree1[now].maxx , val);
else tree2[now].maxx = max(tree2[now].maxx , val);
return ;
}
int mid = (tree1[now].l + tree1[now].r) >> 1;
if(pos <= mid) change(lson , id,pos,val);
else change(rson , id,pos,val);
updata(now , id);
}
int query(int now , int l , int r , int id) {
if(tree1[now].l >= l && tree1[now].r <= r) {
if(id == 1) return tree1[now].maxx;
else return tree2[now].maxx;
}
int mid = (tree1[now].l + tree1[now].r) >> 1 , ans = 0;
if(l <= mid) ans = max(ans , query(lson , l, r, id));
if(r > mid) ans = max(ans , query(rson , l, r, id));
return ans;
}
int num;
signed main() {
// freopen("b.in","r",stdin);
// freopen("b.out","w",stdout);
int n = gi();
rep(i , 1, n) {
a[i].x = gi();
a[i].y = gi();
b[++ num] = a[i].x;
b[++ num] = a[i].y;
}
sort(b + 1,b + num + 1);
for(int i = 1;i <= n;++ i) {
p[i].fi = lower_bound(b + 1,b + num + 1,a[i].x) - b;
p[i].se = lower_bound(b + 1,b + num + 1,a[i].y) - b;
}
build(1 , 1, num);
for(int i = 1;i <= n;++ i) {
if(a[i].x == a[i].y) continue;
f[i] = 1;
if(a[i].x > a[i].y) {
f[i] = max(f[i] , query(1 , 1, p[i].fi - 1, 1) + 1);
change(1 , 1, p[i].se, f[i]);
// printf("%d",)
}else {
f[i] = max(f[i] , query(1 , p[i].fi + 1, num, 2) + 1);
change(1 , 2, p[i].se, f[i]);
}
}
int ans = 0;
for(int i = 1;i <= n;++ i) ans = max(ans , f[i]);
printf("%d",ans);
return 0;
}
T3:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
const int mod = 998244353;
const int maxN = 1e6 + 7;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
struct Edge {
int u , v;
}a[maxN];
struct Node {
int v , nex;
}Map[maxN << 1];
int head[maxN] , num;
PII b[maxN];
void add_Node(int u , int v) {
Map[++ num] = (Node) {v , head[u]};
head[u] = num;
}
int dep[maxN] , f[maxN][23], fa[maxN];
void dfs(int now,int fa) {
dep[now] = dep[fa] + 1;
f[now][0] = fa;
for(int i = 1;i <= 21;++ i) f[now][i] = f[f[now][i - 1]][i - 1];
for(int i = head[now];i;i = Map[i].nex) {
int v = Map[i].v;
if(v == fa) continue;
dfs(v , now);
}
}
int lca(int x , int y) {
int ans = 0;
if(dep[x] > dep[y]) swap(x , y);
for(int i = 21;i >= 0;-- i) {
if(dep[f[y][i]] >= dep[x]) {
y = f[y][i];
ans += 1 << i;
}
}
if(x == y) return ans;
for(int i = 21;i >= 0;-- i) {
if(f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
ans += 1 << (i + 1);
}
}
return ans + 2;
}
int get(int x) {
return fa[x] == x ? fa[x] : fa[x] = get(fa[x]);
}
void Merge(int x , int y) {
fa[get(x)] = get(y);
return;
}
struct Tmp {
int id , x;
}c[maxN];
bool cmp(Tmp a , Tmp b) {
return a.x > b.x;
}
int n;
int main() {
// freopen("c.in","r",stdin);
// freopen("c.out","w",stdout);
n = gi();
for(int i = 1;i < n;++ i) {
a[i].u = gi();
a[i].v = gi();
}
for(int i = 1;i < n;++ i) {
add_Node(a[i].u , a[i].v);
add_Node(a[i].v , a[i].u);
}
dfs(1,0);
for(int i = 1;i <= n;++ i) fa[i] = i;
for(int i = 1;i <= n;++ i) b[i] = mk(i , i);
for(int i = 1;i < n;++ i) {
int x = a[i].u , y = a[i].v;
int x1 = b[get(x)].fi , y1 = b[get(x)].se;
int x2 = b[get(y)].fi , y2 = b[get(y)].se;
Merge(x , y);
c[1] = (Tmp) {1 , lca(x1 , y1)};
c[2] = (Tmp) {2 , lca(x2 , y2)};
c[3] = (Tmp) {3 , lca(x1 , x2)};
c[4] = (Tmp) {4 , lca(x1 , y2)};
c[5] = (Tmp) {5 , lca(y1 , x2)};
c[6] = (Tmp) {6 , lca(y1 , y2)};
sort(c + 1, c + 7,cmp);
if(c[1].id == 1) b[get(x)] = mk(x1,y1);
if(c[1].id == 2) b[get(x)] = mk(x2,y2);
if(c[1].id == 3) b[get(x)] = mk(x1,x2);
if(c[1].id == 4) b[get(x)] = mk(x1,y2);
if(c[1].id == 5) b[get(x)] = mk(y1,x2);
if(c[1].id == 6) b[get(x)] = mk(y1,y2);
printf("%d\n",c[1].x);
}
return 0;
}
10.18
不知道出题人出一堆模板有什么意义?
而且出题人语文不太好,T2什么拐弯,md,我醉了。
T1:模板题
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 200 + 7;
int l1,l2,next[maxN], v[maxN];
char a[maxN][maxN] , b[maxN][maxN];
void get_next(int id){
int p = 0;next[1] = 0;
for(int i = 2;i <= l2;++ i){
while(p > 0 && b[id][i] != b[id][p + 1])p = next[p];
if(b[id][i] == b[id][p + 1])p ++;
next[i] = p;
}
}
long long Sum = 0;
void kmp(int id2,int id) {
int p = 0,ans = 0;
for(int i = 1;i <= l1;++ i){
while(p > 0 && a[id][i] != b[id2][p + 1])p = next[p];
if(a[id][i] == b[id2][p + 1])p ++;
if(p == l2) {
Sum += (i - l2 + 1) * v[id2];
p = next[p];
}
}
}
int main(){
freopen("dream.in","r",stdin);
freopen("dream.out","w",stdout);
int n = gi() , m = gi();
for(int i = 1;i <= m;++ i) scanf("%s",b[i] + 1);
for(int i = 1;i <= m;++ i) v[i] = gi();
for(int i = 1;i <= n;++ i) scanf("%s",a[i] + 1);
for(int i = 1;i <= m;++ i) {
l2 = strlen(b[i] + 1);
get_next(i);
for(int j = 1;j <= m;++ j) {
l1 = strlen(a[j] + 1);
kmp(i,j);
}
}
cout << Sum;
return 0;
}
T2:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define int long long
const int INF = -4485090715960753727;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int gx[] = {0,0,1,-1};
const int gy[] = {1,-1,0,0};
const int maxN = 300 + 7;
int f[25][maxN][maxN];
int a[maxN][maxN];
signed main() {
freopen("corner.in","r",stdin);
freopen("corner.out","w",stdout);
int m = gi() , n = gi();
memset(f , -0x3f,sizeof(f));
for(int i = 1;i <= n;++ i) {
for(int j = 1;j <= m;++ j) {
a[i][j] = gi();
if(a[i][j] == 0) {
f[0][i][j] = 0;
}
}
}
for(int l = 1;l <= 23;++ l) {
for(int i = 1;i <= n;++ i) {
for(int j = 1;j <= m;++ j) {
for(int k = 0;k < 4;++ k) {
if(a[i][j])
f[l][i][j] = max(f[l][i][j] , f[l - 1][i + gx[k]][j + gy[k]] + a[i][j]);
}
}
}
}
int ans = INF;
for(int i = 1;i <= n;++ i) {
for(int j = 1;j <= m;++ j) {
ans = max(ans , f[23][i][j]);
}
}
cout << ans;
return 0;
}
/*
4 4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
*/
T3:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define dbug(x) cout << x << endl
#define mk make_pair
#define fi first
#define se second
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
struct bignum {
int n;
int a[3007];
bignum() {
n = 0;
memset(a, 0, sizeof(a));
}
bignum(string s) {
n = s.size();
memset(a, 0, sizeof(a));
for (int i = 0; i < n; i++)
a[i] = s[n - 1 -i] -'0';
}
bignum(int s) {
memset(a, 0, sizeof(a));
n = 0;
while (s > 0) {
a[n] = s % 10;
s /= 10;
n++;
}
}
void work() {
for (int i = 0; i < n; i++) {
if (a[i] < 0) {
int tmp = (-a[i] - 1) / 10 + 1;
a[i] += 10 * tmp;
a[i + 1] -= tmp;
}
if (a[i] >= 10) {
int tmp = a[i] / 10;
a[i] -= 10* tmp;
a[i + 1] += tmp;
if (i == n - 1 && a[i + 1] > 0) n++;
}
}
while (n > 0 && a[n - 1] == 0) n--;
}
void print() {
for (int i = n - 1; i >= 0; i--)
cout << a[i];
cout << endl;
}
}f[1007],c;
bignum operator + (const bignum &a, const bignum &b) {
bignum c;
c.n = max(a.n, b.n);
for (int i = 0; i < c.n; i++)
c.a[i] = a.a[i] + b.a[i];
c.work();
return c;
}
bignum operator - (const bignum &a, const bignum &b) {
bignum c;
c.n = max(a.n, b.n);
for (int i = 0; i < c.n; i++)
c.a[i] = a.a[i] - b.a[i];
c.work();
return c;
}
bignum operator * (const bignum &a, const bignum &b) {
bignum c;
c.n = a.n + b.n - 1;
for (int i = 0; i < a.n; i++)
for (int j = 0; j < b.n; j++)
c.a[i + j] += a.a[i] * b.a[j];
c.work();
return c;
}
//int ans[50] = {0,0,1,2,9,44,265,1854,14833,133496,1334961};
int main() {
freopen("keke.in","r",stdin);
freopen("keke.out","w",stdout);
f[1] = 0;f[2] = 1;
int n = gi();
for(int i = 3;i <= n;++ i) {
bignum a(i - 1);
f[i] = a * (f[i - 1] + f[i - 2]);
}
f[n].print();
return 0;
}
10.16
预计得分:100 + [30,60] + 30 = [160,190]
实际得分:100 + 50 + 0 = 150
????
我T3忘记一次性处理,继而输出答案了,全场只有我自己写了30????(然后挂分了
T1:
刚开始不会做啊,以为是质数不满足,结果cdx大佬开场1min就切了,强。
后来把素数与素数相乘的数先找出来
4(2 * 2),6(23),9(33)...blablabla
然后看看能凑成什么数,发现在12之后就有长度为4全部满足了,之后肯定满足。
[1,11]内的手玩,发现只有1,3,5,7,11不满足。
特判掉就可以了。
bool check(int x) {
if(x == 1 || x == 5 || x == 7 || x == 11 || x == 2 || x == 3) return true;
return false;
}
signed main() {
int q = gi();
while(q --) {check(gi()) ? puts("0") : puts("1");}
return 0;
}
T2:
我先想了一个\(900 \times n^3 \times q\)的算法,没写,写了一个假n^3DP
然后转瞬间又想到了一个贪心\(900*n*q\)的做法,想到不可能这么简单,就没写。。。。。。(经axm贪心大法怒AT2证明,是正确的....
然后这就接近正解了????
然而我的dp状态不太对吧,233,而且我还比std的二进制运算多了30的常数。
CODE:
#include <bits/stdc++.h>
using namespace std;
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
//#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
inline int gi() {
int x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
}
int q,n,m,ans,x,s[1005],f[1005];
int main(){
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
q = gi();
while(q --) {
n = gi();m = gi();
s[0] = 0;
rep(i , 1, n) {
x = gi();s[i] = s[i - 1] ^ x;
}
ans = 0;
sep(i , 29, 0) {
memset(f,0,sizeof(f));
f[0] = 1;
for (int j = 1;j <= n;++ j){
for (int k = 0;k < j;++ k) if(f[k] > 0){
x = s[j] ^ s[k];
if ( ((ans & x) >= ans) && (( x & ( 1 << i)) > 0) )
f[j] = max(f[j] , f[k]+1);
}
}
if(f[n] > m) ans |= (1 << i);
}
cout << ans << '\n';
}
return 0;
}
T3:
正解不会,这种递推类型的题
状态压缩还是会一点点的。
\(f[S][i]\)表示状态S下价值为i的方案数
转移枚举最后一位是那个数
CODE:
for(int i = 1;i < (1 << n);++ i) {
int len = 0;
for(int j = 1;j <= n;++ j) if(i & (1 << j - 1)) len ++;
for(int j = 1;j <= n;++ j) {
if(i & (1 << j - 1)) {
if(abs(j - len) <= 1) {
for(int k = 1;k <= n;++ k) {
int S1 = i ^ (1 << j - 1);
f[i][k] += f[S1][k - 1];
f[i][k] %= p;
}
}
else {
for(int k = 0;k <= n;++ k) {
int S1 = i ^ (1 << j - 1);
f[i][k] += f[S1][k];
f[i][k] %= p;
}
}
}
}
}
10.15
预计得分:70+50+0
实际得分:30+30+0
???评测机太慢了吧。。。。
修锅后:70+50+0
T1一直在猜是\(O(n)\)算法,然后想不出,心态炸了。
T2是乱搞多得了20,稍微有点大常数,后面都T了。
仍然不会正解
T3不会。
最后20min才写T1 70,也没心思写我的log算法,想log^2和log差不多,就没写log!
写一下自己的log做法吧,很麻烦就是了:
先不考虑加入修改操作
考虑首先将两个序列合并起来。
然后区间\([l1,r1]\)对应着一段区间,区间\([l2,r2]\)对应着一段区间。
两个区间不相交,区间中间值必定在长度大的区间里。
如果相交,那么相交的部分是可以二分的。
考虑加入修改操作
我们把四个端点分别二分就好啦(常数大警告
反正复杂度是\(log_n\)的(小声BB
正解还是比较妙的,分治。
直接粘solution:
普适性更强的方法。假设当前要取的是区间的第k大,将k折半,放在两个区间的对应位置 s,t上,比较 a[s],b[t],不妨设a[s]<b[t],那么答案可以化归至区间[l1,s−1],[l2,r2]的第k2大数(因为a序列比a[s]小的那些数一定可以全部舍去), 递归即可
T1:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int,int>
#define int long long
#define mk make_pair
#define fi first
#define se second
inline int gi() {
int x = 0, f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 5e5 + 7;
int a[maxN], b[maxN];
int kth(int ta[], int sa, int tb[], int sb, int k) {
if (sa > sb) return kth(tb, sb, ta, sa, k);
if (sa == 0) return tb[k];
if (k == 1) return std::min(ta[1], tb[1]);
int ka = std::min(sa, k/2), kb = k - ka;
if (ta[ka] < tb[kb]) return kth(ta+ka, sa-ka, tb, sb, k-ka);
return kth(ta, sa, tb+kb, sb-kb, k-kb);
}
int query(int la, int ra, int lb, int rb) {
int sa = ra-la+1, sb = rb-lb+1, siz = sa + sb;
return kth(a+la-1, sa, b+lb-1, sb, siz/2+1);
}
int main() {
freopen("median.in", "r", stdin);
freopen("median.out", "w", stdout);
int n, m;
n = gi(); m = gi();
for (int i = 1; i <= n; i++) a[i] = gi();
for (int i = 1; i <= n; i++) b[i] = gi();
for (int opt; m--; ) {
opt = gi();
if (opt == 2) {
int la = gi(), ra = gi(), lb = gi(), rb = gi();
printf("%d\n", query(la, ra, lb, rb));
} else {
int p = gi(), pos = gi(), val = gi();
if (p == 0) a[pos] = val;
else b[pos] = val;
}
}
return 0;
}
成绩:
10.13
预计得分:100+100+80
实际得分:100+100+40
这场模拟赛有点无聊
开错题了,把T2当成T1了。
随便猜了个结论,开场5min就写完了!
T1忘记概率怎么搞了,直接用方案数算的。。。。。(核心差不多
T3好题,分析性质,一步步优化到正解,由于没有用到核心性质,常数比其他人大很多。(其他人二分用的都是整形,我是实数)
T3比较难写,先咕着
T1
#include <bits/stdc++.h>
using namespace std;
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define gc getchar()
#define int long long
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 1000 + 7;
int n , m;
double f1[maxN][maxN * 6];
double f2[maxN][maxN * 6];
void Pre() {
f2[0][0] = f1[0][0] = 1;
rep(i , 1, n) {
rep(j , 0, 6 * i) {
for(int k = 1;k <= 6;++ k) {
if(j >= k) f1[i][j] += f1[i - 1][j - k];
}
f1[i][j] /= 6;
}
}
rep(i , 1, m) {
rep(j , 0, 6 * i) {
for(int k = 1;k <= 6;++ k) {
if(j >= k) f2[i][j] += f2[i - 1][j - k];
}
f2[i][j] /= 6;
}
}
}
void Solve() {
double ans = 0;
rep(i , 1, 6 * n) {
rep(j , 1, i - 1) {
ans += f1[n][i] * f2[m][j];
}
}
ans *= 100;
printf("%.2lf%%",ans);
}
signed main() {
n = gi();m = gi();
Pre();
Solve();
return 0;
}
T2
#include <bits/stdc++.h>
using namespace std;
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define gc getchar()
#define int long long
const int maxN = 1e6 + 7;
const int inf = 0x7fffffff;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
struct Node{
int a , b;
}a[maxN];
int sum1[maxN] , sum2[maxN];
bool cmp(Node a , Node b) {
if(a.a == b.a) return a.b < b.b;
return a.a < b.a;
}
signed main() {
int n = gi();
rep(i , 1, n) a[i].a = gi();
rep(i , 1, n) a[i].b = gi();
sort(a + 1, a + n + 1,cmp);
rep(i , 1, n) sum1[i] = a[i].a * a[i].b;
rep(i , 1, n) sum1[i] += sum1[i - 1];
rep(i , 1, n) sum2[i] = a[i].b;
rep(i , 1, n) sum2[i] += sum2[i - 1];
int ans = 0;
rep(i , 1, n) {
int sum = 0;
sum = sum2[i - 1] * a[i].a - sum1[i - 1];
sum += (sum1[n] - sum1[i]) - a[i].a * (sum2[n] - sum2[i]);
if(i == 1) ans = sum;
else ans = min(ans , sum);
}
printf("%lld",ans);
return 0;
}
/*
6
2 1 3 3 2 4
5 1 1 2 2 2
*/
T2得到了正解,
const int maxN = 1e6 + 7;
struct Node {
int a , b;
}a[maxN];
bool cmp(Node a , Node b) {
return a.a < b.a;
}
signed main() {
int n = gi();
rep(i , 1, n) a[i].a = gi();
rep(i , 1, n) a[i].b = gi();
sort(a + 1,a + n + 1,cmp);
int ans = 0 , sum_b = 0, cur = 0;
rep(i , 1, n) cur += a[i].b * (a[i].a - a[1].a);
ans = cur;
int k1 = 0, k2 = 0;
rep(i , 2, n) k1 += a[i].b;k2 = a[1].b;
rep(i , 2, n) {
cur -= k1 * (a[i].a - a[i - 1].a);
cur += 1ll * k2 * (a[i].a - a[i - 1].a);
k1 -= a[i].b;k2 += a[i].b;
ans = min(ans , cur);
}
cout << ans;
return 0;
}
T3
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 1000010;
const int MAXL = 22;
int rmin[MAXL][MAXN], rmax[MAXL][MAXN];
int n, q;
int a[MAXN];
inline void init_rmq() {
for (int i = 1; i < MAXL; i++) {
for (int j = 0; j + (1 << i) <= n; j++) {
rmin[i][j] = min(rmin[i-1][j], rmin[i-1][j+(1<<(i-1))]);
rmax[i][j] = max(rmax[i-1][j], rmax[i-1][j+(1<<(i-1))]);
}
}
}
inline bool test(int d, int m) {
int l = 0, i;
for (i = 0; i < m && l < n; i++) {
int r = l, smin = 1000000001, smax=0;
for (int j = MAXL - 1; j >= 0; j--) {
if (r + (1 << j) <= n) {
int tmin = rmin[j][r], tmax = rmax[j][r];
if (max(smax, tmax) - min(smin, tmin) <= d) {
smax = max(smax, tmax);
smin = min(smin, tmin);
r += (1 << j);
}
}
}
l = r;
}
return l == n;
}
inline int work(int m) {
int l = 0, r = 1000000001;
while (l < r) {
int mid = (l + r) >> 1;
if (test(mid, m)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%*d%d", &rmin[0][i]);
rmax[0][i] = rmin[0][i];
}
init_rmq();
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int m;
scanf("%d", &m);
int ans = work(m);
if (ans % 2) printf("%d.5\n", ans/2);
else printf("%d\n", ans/2);
}
}
10.5
可能是因为坚持一早自习没睡觉,考试特别困。然后一点考试的状态都没有。
连基础的状态压缩都搞不到手了,没开longlong,题意读错导致数组问题等出现了很多问题。
没有合理安排时间,写第一题代码我记得是10点。
预计得分:100 + 22 + 0
实际得分:0+2+0
考完心态爆炸了
仅仅只有T1代码剩下两道题没补
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define int long long
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 3e3 + 7;
int sum[maxN][maxN];
int t1[maxN][maxN];
int t2[maxN][maxN];
int t3[maxN][maxN],t4[maxN][maxN];
int n , q;
void solve() {
for(int i = 1;i <= n;++ i) {
for(int j = 1;j <= n;++ j) {
sum[i][j] = sum[i][j] + sum[i][j - 1] + t1[i][j] + t4[i][j];
}
}
int ans = 0;
rep(i , 1, n) {
rep(j , 1, n) {
ans ^= sum[i][j];
}
}
cout << ans;
}
void work() {
rep(i , 1, n) {
rep(j , 1, n) {
t1[i][j] = t1[i - 1][j] + t2[i][j];
}
}
rep(i , 1, n) {
rep(j , 1, n) {
t4[i][j] = t4[i - 1][j - 1] + t3[i][j];
}
}
return ;
}
signed main() {
// freopen("u.in","r",stdin);
// freopen("u.out","w",stdout);
n = gi();q = gi();
while(q --) {
int r = gi() , c = gi(),l = gi(),s = gi();
t2[r][c] += s;
t2[r + l][c] -= s;
t3[r][c + 1] -= s;
t3[r + l][c + l + 1] += s;
}
work();
solve();
return 0;
}
10.6
考过的模拟赛,T2是个假题,T3神仙题。
花几分钟写了T1,T3 60,然后就开始看别的课件了。
神仙学弟sjp搜索踩爆状态压缩了,很裸很裸的那种搜索。
牛皮
T2就不补了
T1:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
inline int gi() {
int x = 0, f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 1e5 + 7;
struct Node {
int l , x, v;
}a[maxN];
int main() {
freopen("cruise.in","r",stdin);
freopen("cruise.out","w",stdout);
int T = gi();
while(T --) {
int n = gi();
rep(i , 1, n + 1) a[i].l = gi();
rep(i , 1, n + 1) a[i].x = gi();
rep(i , 1, n + 1) a[i].v = gi();
// double t = 1.0 * (a[n + 1].x + a[n + 1].l) / a[n + 1].v;
// for(int i = n;i >= 2;-- i) {
// int L = a[i].v * t;
// if(L >= a[i].x) {
// t += 1.0 * a[i].l / a[i].v;
// }else {
// t = 1.0 * (a[i].x + a[i].l) / a[i].v;
// }
// }
//// printf("%lf\n",t);
// int L = a[1].v * t;
// if(L + a[1].l <= a[1].x) {
// t += 1.0 * a[1].l / a[1].v;
// }else {
// t = 1.0 * a[1].x / a[1].v;
// }
// printf("%lf",t);
double ans = 0;
double sum = 0;
for(int i = 1;i <= n + 1;i++) {
if(i != 1) sum += a[i].l;
double t = (a[i].x + sum) / a[i].v;
ans = max(t,ans);
}
printf("%.10f\n",ans);
}
return 0;
}
T3:
#include <bits/stdc++.h>
using namespace std;
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
const int maxN = 10 + 7;
char a[maxN][maxN];
int f[maxN][maxN][maxN][maxN];
int calc(int x1, int y1, int x2, int y2, int i1, int j1, int i2, int j2) {
int res = 0;
for (int i = i1; i <= i2; ++ i)
for (int j = j1; j <= j2; ++ j)
if (a[i][j] == '#')
res += max(max(abs(i-x1), abs(i-x2)), max(abs(j-y1), abs(j-y2)));
return res;
}
int dp(int x1 , int y1, int x2 , int y2) {
if(x1 > x2 || y1 > y2) return 0;
int &res = f[x1][y1][x2][y2];
if(res > -1) return res;
res = calc(x1, y1, x2, y2, x1, y1, x1, y2) + dp(x1+1, y1, x2, y2);
res = min(res, calc(x1, y1, x2, y2, x2, y1, x2, y2) + dp(x1, y1, x2-1, y2));
res = min(res, calc(x1, y1, x2, y2, x1, y1, x2, y1) + dp(x1, y1+1, x2, y2));
res = min(res, calc(x1, y1, x2, y2, x1, y2, x2, y2) + dp(x1, y1, x2, y2-1));
return res;
}
int main() {
memset(f , -1, sizeof(f));
rep(i , 1, 8) scanf("%s",a[i] + 1);
printf("%d",dp(1,1,8,8));
return 0;
}
10.11普及模拟赛
普及组的题目,比较简单。
昨天晚上忘记有这个比赛了,最后1h才想起来去看了看题,想了想。
想是想出来了,但是写不动T2,就只写了T1,T2.
T1
给定x,y,a,b(都是在数轴上的点
开始点在x,求到y的最小距离。
有一对传送点(a,b),能从a不花费任何距离到b,或从b不花费任何距离到a.
让x<y,a<b.然后判断走不走(a,b)哪个更优
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
inline int gi() {
int x = 0, f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
int main() {
int x = gi() , y = gi(), a = gi(), b = gi();
if(x > y) swap(x , y);
if(a > b) swap(a , b);
int ans = y - x;
ans = min(ans , abs(x - a) + abs(y - b));
printf("%d",ans);
return 0;
}
T2
很长的模拟题面
模拟就行了,额
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int , int>
#define mk make_pair
#define fi first
#define se second
#define CL(x) memset(x , 0, sizeof(x));
#define int long long
const int maxN = 1e7 + 7;
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
int a[10];
int b[10] , c[10], d[10];
signed main() {
int n;
while(scanf("%lld",&n) && n) {
int q = sqrt(n * 10);
CL(b);
int tmp = n;
while(tmp) {b[tmp % 10] ++;tmp /= 10;}
if(b[0] > 0) {
printf("0 * 0 = 0\n");
}
for(int i = 1;i < q;++ i) {
int tmp = i;
CL(a);
while(tmp) a[tmp % 10] ++ , tmp /= 10;
bool flag = true;
rep(j , 0, 9) {
if(a[j] > b[j]) flag = false;
}
if(flag) {
int x = i * i;
CL(d);
while(x) {
d[x % 10] ++;
x /= 10;
}
bool flag = true;
rep(j , 0, 9) if(d[j] > b[j]) flag = false;
if(flag) printf("%lld * %lld = %lld\n",i,i,1ll * i * i);
}
}
}
return 0;
}
T3
给定一个背包容量m和很多物品,求多少方案使得\(m - min(不选的物品) < 选择物品之和\)
从小到大排序,枚举最小不选的物品是哪一个,然后先把前面的都选了,然后背包求方案数
注意全都选择的情况。
#include <bits/stdc++.h>
using namespace std;
#define gc getchar()
#define rep(i , x, y) for(int i = x;i <= y;++ i)
#define sep(i , x, y) for(int i = x;i >= y;-- i)
#define PII pair<int , int>
#define mk make_pair
#define fi first
#define se second
#define CL(x) memset(x , 0, sizeof(x));
inline int gi() {
int x = 0,f = 1;char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
return x * f;
}
const int maxN = 30 + 7;
const int maxM = 1000 + 7;
int n , m, a[maxN], f[maxN][maxM];
bool cmp(int a , int b) {return a > b;}
void Read() {
n = gi();m = gi();
int sum = 0;
rep(i , 1, n) a[i] = gi();
sort(a + 1, a + n + 1);
}
void Solve() {
int sum = 0, ans = 0;
rep(i , 1, n) sum += a[i];
if(sum <= m){puts("1");return;}
rep(i , 1, n) {
sum = 0;
CL(f);
rep(j , 1, i - 1) sum += a[j];
if(sum > m) break;
f[i][sum] = 1;
rep(k , i + 1, n) {
rep(j , 0, m) {
f[k][j] = f[k - 1][j];
if(j >= a[k]) f[k][j] += f[k - 1][j - a[k]];
}
}
rep(j , m - a[i] + 1, m) {
ans += f[n][j];
}
}
printf("%d\n",ans);
return ;
}
int main() {
int T = gi();
while(T --) {
Read();
Solve();
}
return 0;
}
T4:
比较容易想到的套路题吧。
首先离散化,然后按照询问的大小从大到小排列,直接并查集合并。