2022 第十三届蓝桥杯大赛软件赛省赛(第二场),C/C++ 大学B组题解
补题链接:地址
#include
using namespace std;
int main()
{
// 请在此输入您的代码
cout<<"ABC";
return 0;
}
题意:求第一个大于某2e8的数的回文数,且满足他可以等于1+2+…某个k。
一种是从2e8开始往上枚举,判断每个数是不是回文+三角,三角可以根号再乘附近的几个数特判一下。
另一种是枚举三角,1+2+…k,判断是不是回文和大于2e8,+k加到后面就是指数级了,上升也很快。
答案:35133153
#include
using namespace std;
int main(){
int x = 0;
for(int i = 1; ; i++){
x += i;
if(x <= 20220514)continue;
string s = to_string(x);
string t = s;
reverse(t.begin(),t.end());
if(s==t)break;
}
cout<<x<<"\n";
return 0;
}
#include
using namespace std;
int main(){
int n; cin>>n;
int i, x = 0;
for(i = 1; ; i++){
x += i;
if(x < n)continue;
break;
}
cout<<i<<"\n";
return 0;
}
#include
using namespace std;
int main(){
int n; cin>>n;
set<int>se;
for(int i = 1; i <= n; i++){
string t; int id; cin>>t>>id;
se.insert(id);
}
for(auto x : se){
cout<<x<<"\n";
}
return 0;
}
#include
using namespace std;
const int maxn = 1e5+10;
int a[maxn], f[maxn];
int isprime(int x){ //判断素数
for(int i = 2; i*i <= x; i++){
if(x%i==0)return 0;
}
return 1;
}
int find(int x){//找最小质因数
if(x==0 || x==1)return x;
for(int i = 2; i*i <= x; i++){
if(x%i==0 && isprime(i))return i;
}
}
int main(){
int n; cin>>n;
for(int i = 1; i <= n; i++)cin>>a[i];
memset(f, 0xc0, sizeof(f));
f[1] = a[1];
for(int i = 1; i <= n; i++){
int x = i+find(n-i); //最小质因数
for(int j = i+1; j <= x; j++){
f[j] = max(f[j], f[i]+a[j]);
}
}
cout<<f[n]<<"\n";
return 0;
}
#include
using namespace std;
const int maxn = 510;
int a[maxn][maxn], vis[maxn][maxn];
struct node{int x, y, time;}; //完成变色的时间
bool operator < (node a, node b){ return a.time > b.time; }
priority_queue<node, vector<node>, less<node> >q;
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
int main(){
int n, m; cin>>n>>m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin>>a[i][j];
}
}
int res = 0;
q.push((node){1, 1, a[1][1]});
vis[0][0] = 1;
while(q.size()){
node t = q.top(); q.pop();
res = max(res, t.time);
for(int i = 0; i < 4; i++){
int nx = t.x+dx[i], ny = t.y+dy[i];
if(nx>=1&&nx<=n && ny>=1&&ny<=m && vis[nx][ny]==0){//没染过色
vis[nx][ny] = 1;
q.push((node){nx,ny, t.time+a[nx][ny]});
}
}
}
cout<<res<<"\n";
return 0;
}
#include
using namespace std;
typedef long long LL;
map<int,vector<LL>>mp; //mp[x]: 维护余数为x的前缀和有哪些
LL s[4]; //前缀和, 滚动数组
int main(){
int n, k; cin>>n>>k;
LL ans = 0;
mp[0].push_back(0); //别忘了
for(int i = 1; i <= n; i++){
int x; cin>>x;
s[i%2] = s[(i-1+2)%2]+x;
int r = (s[i%2]%k+k)%k; //%k余数
int len = mp[r].size();
for(int j = 0; j < len; j++){
if(mp[r][j] <= s[i%2])ans++;
}
// sort(mp[r].begin(), mp[r].end());
// ans += upper_bound(mp[r].begin(), mp[r].end(), s[i%2])-mp[r].begin();
mp[r].push_back(s[i%2]);
}
cout<<ans<<"\n";
return 0;
}
//分块可以AC 20个点的块长, sqrt(n)*5
#include
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n; cin>>n;
vector<int> a(n+1,0);
//分块
int len = sqrt(n)*5; //块长
int k = (n%len==0)?n/len:n/len+1;//块数
vector<int> block[k+1]; //本体
vector<int> belong(n+1,0);
int blockNum = 0;
for(int i=1;i<=n;i++){ //分块
cin>>a[i];
blockNum=(i-1)/len+1;
belong[i]=blockNum;
block[blockNum].push_back(a[i]);
}
for(int i=1;i<=k;i++){ //块排序
sort(block[i].begin(),block[i].end(),less<int>());
}
//输入操作
int m; cin>>m;
vector<vector<int>> op(m,vector<int>(4,0));
for(int i=0;i<m;i++){
cin>>op[i][0];
cin>>op[i][1]>>op[i][2];
if(op[i][0]==2)cin>>op[i][3];
}
//执行
vector<int> res;
for(int i = 0; i < m; i++){
int num1=op[i][1], num2=op[i][2], num3=op[i][3];
if(op[i][0]==1){ //修改
auto it=lower_bound(block[belong[num1]].begin(),block[belong[num1]].end(),a[num1]);
block[belong[num1]].erase(it);
it=lower_bound(block[belong[num1]].begin(),block[belong[num1]].end(),num2);
if(it==block[belong[num1]].end())block[belong[num1]].push_back(num2);
else block[belong[num1]].insert(it,num2);
a[num1] = num2;
}else{ //查询
int count = 0, mid = a[num3];
//先查左右两端分块中满足条件的元组数,因为num1和num2所在的块不一定一整块都参与比较
for(int j=num1;j<=min(num2,belong[num1]*len);j++){
if(a[j]<mid)count++;
}
if(belong[num1]!=belong[num2]){
for(int j=(belong[num2]-1)*len+1;j<=num2;j++){
if(a[j]<mid)count++;
}
}
//区间查询,用二分法查询每个块中小于a[p]的元素个数
for(int j=belong[num1]+1;j<=belong[num2]-1;j++){
count+=lower_bound(block[j].begin(),block[j].end(),a[num3])-block[j].begin();
}
res.push_back(count+1);
}
}
for(auto &&num:res)cout<<num<<" ";
return 0;
}
#include
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
LL pows(LL a, LL x, LL p){if(x==0)return 1; LL t = pows(a, x>>1,p);if(x%2==0)return t*t%p;return t*t%p*a%p;}
int main(){
int n, m; cin>>n>>m;
int nn = sqrt(2*m)+2;
while(nn*(nn+1)>2*m)nn--;
n = min(n, nn);
//dp
vector<vector<LL>>f(n+1, vector<LL>(m+1));
LL res = 1;
for(int i = 0; i <= m; i++)f[1][i] = 1;
for(int i = 2; i <= n; i++){
for(int j = 1; j <= m; j++){
if(i>=j)continue;
f[i][j] = (f[i][j-i]+f[i-1][j-i])%mod;
}
}
for(int i = 2; i <= n; i++){
res = (res+(f[i][m]*pows(2, i-1, mod))%mod)%mod;
}
cout<<res<<"\n";
return 0;
}