A. Fibonacciness
暴力判断即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=1e5+7, mod=1e9+7;
int cnt=0,a[10];
void solve(){
for(int i=1;i<=5;i++){
if(i==3)continue;
else cin>>a[i];
}
cnt=0;
int ans=0;
a[3]=a[1]+a[2];
for(int i=3;i<=5;i++){
if(a[i]==a[i-1]+a[i-2])cnt++;
}
ans=max(ans,cnt);
cnt=0;
a[3]=a[5]-a[4];
for(int i=3;i<=5;i++){
if(a[i]==a[i-1]+a[i-2])cnt++;
}
ans=max(ans,cnt);
cnt=0;
a[3]=a[5]-a[2];
for(int i=3;i<=5;i++){
if(a[i]==a[i-1]+a[i-2])cnt++;
}
ans=max(ans,cnt);
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
|
B. Farmer John’s Card Game
读漏题了,多吃一铂多罚时。
中间那堆卡并不会重置,每轮结束最上面的卡不会清零
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=1e5+7, mod=1e9+7;
struct Node{
int x,y;
bool operator< (const Node t){
return x<t.x;
}
};
int n,m;
int a[2005][2005];
Node ans[2005];
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)cin>>a[i][j];
sort(a[i]+1,a[i]+1+m);
}
for(int i=1;i<=n;i++){
ans[i]=Node({a[i][1],i});
}
sort(ans+1,ans+1+n);
int now=ans[n].x;
for(int i=2;i<=m;i++){
Node tmp[m+10];
for(int j=1;j<=n;j++){
tmp[j]=Node({a[j][i],j});
}
sort(tmp+1,tmp+1+n);
for(int j=1;j<=n;j++){
if(ans[j].y!=tmp[j].y||tmp[j].x<now){
cout<<"-1\n";
return;
}
}
now=tmp[n].x;
}
for(int i=1;i<=n;i++)cout<<ans[i].y<<" ";
cout<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
|
C. Game of Mathletes
不能成对的个数为奇,则答案为成对对数-1,否则为成对对数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=2e5+7, mod=1e9+7;
int n,k;
int x[N];
void solve(){
cin>>n>>k;
int ans=0;
map<int,int>vis;
for(int i=1;i<=n;i++){
cin>>x[i];
}
for(int i=1;i<=n;i++){
if(vis[x[i]] > 0){
vis[x[i]]--;
ans++;
} else {
vis[k-x[i]]++;
}
}
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
|
D. Subtract Min Sort
贪心硬判即可
能减则减,前比后小就减,否则就是 NO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=2e5+7, mod=1e9+7;
int n,a[N];
void solve(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<n;i++){
if(a[i+1]>=a[i]){
a[i+1]-=a[i];
a[i]=0;
}
else{
cout<<"NO\n";
return;
}
}
for(int i=1;i<n;i++){
if(a[i]>a[i+1]){
cout<<"NO\n";
return;
}
}
cout<<"YES\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
|
E. Graph Composition
无法战胜的图论
赛时想到联通块构造解决,写半天没写出来
对于 G 中的每一个联通块,都要在 F 中构造出来
用 DFS 先把 G 中所有的联通块找出来,然后开始构造 F
先判断 F 中的边,如果在 G 的联通块中,这条边连接的两个点不是同一联通块的,就删除,反之则并到一起
接着判断少的边,判断每块联通块中缺少的点,加边
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=2e5+7, mod=1e9+7;
int n,m1,m2,fa[N],tf[N],vis[N],tot;
pair<int,int>edge[N];
vector<int>e[N],t[N];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void dfs(int x){
if(vis[x])return;
vis[x]=1;
t[tot].push_back(x);
tf[x]=tot;
for(auto v:e[x])dfs(v);
}
void solve(){
int ans=0;
tot=0;
cin>>n>>m1>>m2;
for(int i=1;i<=n;i++){
e[i].clear();
vis[i]=0;
fa[i]=i;
tf[i]=0;
}
for(int i=1;i<=m1;i++){
int u,v;
cin>>u>>v;
edge[i]={u,v};
}
for(int i=1;i<=m2;i++){
int u,v;
cin>>u>>v;
e[u].push_back(v);
e[v].push_back(u);
}
for(int i=1;i<=n;i++){
if(!vis[i]){
tot++;
t[tot].clear();
dfs(i);
}
}
for(int i=1;i<=n;i++){
vis[i]=0;
}
for(int i=1;i<=m1;i++){
int u,v;
u=edge[i].first,v=edge[i].second;
if(tf[u]!=tf[v])ans++;
else fa[find(u)]=find(v);
}
for(int i=1;i<=tot;i++){
for(int j=1;j<t[i].size();j++){
if(find(t[i][j-1])!=find(t[i][j])) fa[find(t[i][j])]=find(t[i][j-1]),ans++;
}
}
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
|
F. Multiplicative Arrays
固定乘积为 x,当数组大小很大时,绝大多数元素都将为 1,由其他非 1 元素构成乘积 x。
显然非 1 的元素个数不超过 $\lfloor \log x \rfloor$ 个。
用 DP 来解决。
$dp_{i,j}$ 第一维为乘积大小,第二维为数组大小。(这里的大小只指非 1 元素)
转移方程如下:
$dp_{i*d,j+1} = dp_{i,j}$
通过此 DP 可以得到每种 x 对应的数组个数,剩下的就是往其中插入 1,此时为一个插空问题,用组合数计算即可。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <bits/stdc++.h>
using namespace std;
#define ls now<<1
#define rs now<<1|1
#define endl "\n"
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int N=1e5+7, mod=998244353;
ll qpow(ll a, ll b, ll m) {
ll res = 1;
while(b) {
if(b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll comp(int n, int m) {
if(n < m || m < 0) return 0;
ll res = 1;
for(int i = 1; i <= m; i++) {
res = res * (n - i + 1) % mod;
}
ll inv = 1;
for(int i = 1; i <= m; i++) {
inv = inv * qpow(i, mod-2, mod) % mod;
}
return res * inv % mod;
}
ll n,k;
ll f[N][20];
void solve(){
cin>>k>>n;
for(int i=1;i<=k;i++){
for(int j=1;j<19;j++)f[i][j]=0;
}
for(int i=2;i<=k;i++){
f[i][1]=1;
for(int j=1;j<19;j++){
for(int l=2;l*i<=k;l++){
f[i*l][j+1]+=f[i][j];
}
}
}
for(int i=1;i<=k;i++){
ll sum=0;
for(int j=1;j<=19;j++){
sum=(sum+(comp(n+1,j+1)*f[i][j]))%mod;
}
if(i==1)sum=n;
cout<<sum<<" ";
}
cout<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
|