跳到正文

题解

B3812 std::string 题解

Contexpr约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年7月13日
更新
2026年7月13日
篇幅
约 3 分钟
题目
B3812

版本

  1. v1当前

B3812 std::string 题解

Hack 的方法:

1:1: 负数!

2:2: 炸数组(越界)!

3:3: 超时!

要领:

看数据范围!

进入正题:

Problem 11:

你需要 Hack 的 Code:

C++
#include <iostream>
using namespace std;

int main() {
  int n;
  string ans, base = "HelloLuogu";
  cin >> n;
  while (n--) ans = ans + base;
  cout << ans << endl;
}

(属于用不正常的方法做一个入门题)

非常的明显,当 nn 极大的时候,此程序会超时(炸掉),在本地 Dev-C++ 一试,1010 秒了,还是没有响应,所以 Problem 11 的代码大家应该知道了吧~~~

Problem 22:

你需要 Hack 的 Code:

C++
#include <vector>
#include <iostream>
#include <algorithm>

int T;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  std::cin >> n;
  std::vector<int> a(n + 1), pre(n + 2), post(n + 2);
  for (int i = 1; i <= n; ++i) std::cin >> a[i];
  pre[1] = a[1];
  for (int i = 2; i <= n; ++i) pre[i] = pre[i - 1] & a[i];
  post[n] = a[n];
  for (int i = n - 1; i; --i) post[i] = post[i + 1] & a[i];
  int ans = 1;
  for (int i = 1; i < n; ++i) if (pre[i] + post[i + 1] == pre[n]) {
    ++ans;
  }
  std::cout << ans << '\n';
}

(稍微有点难)

Problem 22 想要炸数组是不可能滴,因为有 vector,炸不了啊~~~

但我们发现第二个循环,是从 22 ∼∼ nn 的,所以只要输入一个 00 程序就会 WA 掉(即 Wrong Answer)

代码

(你们最喜欢的部分来喽~)

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	int x;
	cin>>x;
	if(x==1) cout<<100000;
	else if(x==2){
		cout<<6<<endl;
        cout<<114514<<" "<<0<<" "<<22332<<" "<<1000000000<<" "<<98898<<" "<<65536;
	}else{//this code will not be executed
        cout<<"I AK IOI!!!!!!";
    }
	return 0;
}
原文