牛客周赛 Round 49

A

普通的一个模拟。

1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
typedef long long ll;
const ll mod = 1e9 + 7;
int main()
{
ll a, b;
cin>>a>>b;
cout << (a - b) - (b * 10) << endl;
return 0;
}

B

非常优雅的递归写法,可以明显的发现是符合递归的规律的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
#define int long long

int f(int x)
{
if(x == 1)return 1;
return 2 * f(x / 2) + 1;
}

signed main()
{
int x;
cin >> x;
cout << f(x) << '\n';

}

C

最大连续子段和变一下而已,套路题目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,m;
long long f[N],a[N];

int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i],a[i]-=m;
long long ma=0;
for(int i=1;i<=n;i++)
{
f[i]=max(f[i-1]+a[i],a[i]);
if(f[i]<0) f[i]=0;
ma=max(ma,f[i]);
}
cout<<ma;
return 0;
}

D

异或明显是有一个前缀和性质在的。因此我们可以用前缀和的思路进行处理这一道题目。

除此之外的话,异或我们可以发现是以4为周期的。

如果是

1->1

1,2->2+1

1,2,3->0

1,2,3,4->4

因此就可以写出一段优雅的代码:

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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
long long sumXor(long long x)
{
if (x % 4 == 0)
{
return x;
}
if (x % 4 == 1)
{
return 1;
}
if (x % 4 == 2)
{
return x + 1;
}
return 0;
}

int main(){
ll t;
cin >> t;
while(t--)
{
ll l, r;
cin >> l >> r;
cout << (sumXor(l - 1) ^ sumXor(r)) << '\n';
}
return 0;
}

E

带进去化简一下,然后就请开始进行九年级学生都会的分类讨论罢。

  • 非方程
    • 无解
    • 无数组解
  • 一元一次方程
    • 一组解
  • 二元一次方程
    • 两组解
    • 一组解
    • 无解
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
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define i128 double
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
ll a1, b1, c1, a2, b2, c2;
cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2;
i128 a=a1*b2,b=b1*b2+a2,c=b2*c1+c2;
if(a==0)
{
if(b==0)
{
if(c==0)
{
cout<<"INF\n";
}
else
{
cout<<0<<endl;
}

}
else{
cout<<1<<endl;
}
}
else{
i128 delta=b*b-4*a*c;
int t=1;
if(delta>0)
t=2;
if(delta<0)
t=0;
cout<<t<<endl;
}


}

return 0;
}

F

大概率就是哈希做法了。

一份很优雅的随机模数哈希。

和满足等差数列,可以用字符串哈希。

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;
using i64 = long long;
using pii = std::pair<int, int>;
using u64 = unsigned long long;
bool isprime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}

int findPrime(int n) {
while (!isprime(n)) {
n++;
}
return n;
}
void solve()
{
mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
const int P = findPrime(rng() % 900000000 + 100000000);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i ++) {
std::cin >> a[i];
}
vector<u64> p(n + 1), h(n + 1);
p[0] = 1;
for (int i = 0; i < n; i ++) {
p[i + 1] = p[i] * P;
h[i + 1] = h[i] * P + a[i];
}
auto find = [&](int l, int r) -> u64
{
return h[r] - h[l - 1] * p[r - l + 1];
};
for (int k = 1; k <= n; k ++)
{
if (find(1, n - 2 * k) + find(2 * k + 1, n) == 2 * find(k + 1, n - k)) {
std::cout << k << '\n';
return;
}
}
}
int main()
{
int T = 1;
while (T --) solve();
return 0;
}