#include<iostream>
using namespace std;
int fastpow(int a,int b,int p){
int ans=1;a=a%p;
for(int i=0;i<=31;i++){
if(b &(1<<i)) ans=ans*a%p;
a=a*a%p;
}
return ans;
}
int main(){
int a,b,p;
cin>>a>>b>>p;
cout<<fastpow (a,b,p);
return 0;
}
第7、8行a*a两侧加括号无必要。
{{ select(16) }}
√
×
交换7、8行代码输出不变。
{{ select(17) }}
√
×
将循环上限缩小至10,任意a,p输入结果仍正确。
{{ select(18) }}
√
×
缩小p范围不影响答案正确性。
{{ select(19) }}
√
×
a=2,b=15,输出不可能为。
{{ select(20) }}
16068
16086
16049
16091
说法正确的是。
{{ select(21) }}
答案与a奇偶性一定相同
a变小输出一定变小
a=2且b≤30结果一定正确
算法复杂度O(log2n)
阅读程序2 可一段乘x的最大连续子段和
#include<iostream>
using namespace std;
int main(){
int n,x;
cin>>n>>x;
int a=0,b=0,c=0,na,nb,nc,ans=0;
for(int i=1;i<=n;i++){
int now;
cin>>now;
na =max (a+now,0);
nb =max(max (a+now*x,b+now*x),0);
nc =max (c+now,b+now,0);
a=na,b=nb,c=nc;
ans =max (max (ans,a),max (b,c));
}
cout<<ans;
return 0;
}
程序共输入n+3个数字。
{{ select(22) }}
√
×
朴素无优化DP。
{{ select(23) }}
√
×
变量a代表前i个最大连续子序列和。
{{ select(24) }}
√
×
给定范围int可能溢出出错。
{{ select(25) }}
√
×
程序对应题意。
{{ select(26) }}
序列可将一段全部乘x,求最大连续子段和
最多x个元素加x求最大子段
任意元素加x求最大子段
长度不超过x的最长子段和
输出结果最大输入组。
{{ select(27) }}
5 3 1 2 0 -2 5
3 10 1 -2 1
7 1 1 2 -1 1 2 -1 5
6 -1 1 2 3 4 5 6
阅读程序3 状压DP最长哈密顿路
#include<bits/stdc++.h>
using namespace std;
typedef long long int_t;
int_t dis[1<<18][18];
struct E{
int_t to,w;
E(int_t to, int_t w):to(to),w(w){}
};
vector<E>G[20];
int_t dfs (int_t rt,int_t vised, int_t n){
if(rt==n-1) return 0;
if(dis[vised][rt]) return dis[vised][rt];
dis[vised][rt]=-998244353;
for (Ee: G[rt]){
int_t to=e.to,w=e.w;
if((1<<to) &vised) continue;
dis[vised][rt]=max(dis[vised][rt],dfs (to,vised|(1<<to),n) +w);
}
return dis[vised][rt];
}
int main(){
int_t n,m;cin>>n>>m;
while (m--){
int_t u,v,w;
cin>>u>>v;
G[u].push_back (E(v,w));
}
cout<<dfs (0,1,n);
return 0;
}
NOIP标准可编译。
{{ select(28) }}
√
×
可处理重边自环。
{{ select(29) }}
√
×
可使用Dijkstra替代该算法。
{{ select(30) }}
√
×
错误说法。
{{ select(31) }}
31行为u添加边(v,w)
删除17行复杂度不变
删除21行会死循环
19遍历rt所有出边
输入:3条边 0 2 5 / 0 1 4 / 1 2 3,输出。
{{ select(32) }}
3
5
7
8
算法时间复杂度。
{{ select(33) }}
O(n2+m)
O(2n+m)
O(2n(n+m))
O(n2logn+m)
三、完善程序(每题3分,共30分)
完善程序1 QQ禁言贪心
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
int big[MAXN],small[MAXN], sum[MAXN];
int p1=1,p2=1;
int n,m,k,x;
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
cin>>x;
if(x<=k){
①
} else {
big[p2++]=x;
}
}
sort (small+1, small+1+p1,greater<int>());
sort (big+1,big+1+p2,greater<int>());
for(int i=1;i<=p1;i++){
②
}
int ans =sum[p1],cur=0;
for(int i=1;i<=p2;i++){
cur+=big[i];
int days= ③;
if (days>n){
break;
}
int left=min (n-days,p1);
ans =max (ans,④);
}
cout<<ans<<endl;
return 0;
}