【牛客网】牛客OI赛制测试赛3

版权声明:转载请联系QQ:1810647785 https://blog.csdn.net/weixin_42619451/article/details/82820279

A A 数字权重

链接:https://www.nowcoder.com/acm/contest/189/A
来源:牛客网

题目描述

小a有一个n位的数字,但是它忘了各个位上的数是什么,现在请你来确定各个位上的数字,满足以下条件:
设第 i i 位的数为 a i a_i ,其中 a 1 a_1 为最高位, a n a_n 为最低位, K K 为给定的数字。

  1. 不含前导 0 0
  2. ( i = 2 n ( a i a i 1 ) ) = K (\sum_{i=2}^{n}(a_i-a_{i-1}))=K
    请你求出满足条件的方案数。

输入描述

两个整数 N , K N, K
若存在无解的情况,请输出 0 0

输出描述

一个整数表示答案,对 1 0 9 + 7 10^9+7 取模。

实例1

输入

2 3

输出

6

说明

满足条件的数有:14,25,36,47,58,69

实例2

输入

2 -3

输出

7

说明

满足条件的数有:41,52,63,74,85,96,30

实例3

输入

4 3

输出

600

说明

可能的方案有:1234,1334

实例4

输入

4 -3

输出

700

备注

对于 30 % 30\% 的数据: n , k = 5 n, |k| = 5
对于 60 % 60\% 的数据: n , k 1000 n, |k| ≤ 1000
对于 100 % 100\% 的数据: n , k 1 0 1 3 n, |k| ≤ 10^13
保证 n > 1 n > 1

题解

**正解:**我们化简条件 2 2 的式子后发现,只要让最低为比最高位大 K K 就是可行解。所以我们枚举最高位和最低位。可行解与中间数字的大小无关,所以用快速乘求出中间数字的方案数即可。注意会爆 int

实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int mod = 1e9 + 7;

ll read() {
	ll x = 0, f = 1; char c = getchar();
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
	return f * x;
}

int cnt;
ll n, k;

ll pw(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b & 1) ret = ret * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ret;
}

int main() {
	n = read(), k = read();
	for(int i = 1; i <= 9; i++) {
		for(int j = 0; j <= 9; j++) {
			if(j - i == k) cnt++;
		}
	}
	cout << 1ll * cnt * pw(10, n - 2) % mod <<endl;
}

B B 毒瘤 X O R XOR

链接:https://www.nowcoder.com/acm/contest/189/B
来源:牛客网

题目描述

小a有 N N 个数 a 1 , a 2 , . . . , a N a_1, a_2, ..., a_N ,给出 q q 个询问,每次询问给出区间 [ L , R ] [L, R] ,现在请你找到一个数 X X ,使得

  1. 0 X 2 31 0 ≤ X ≤ 2^{31}
  2. i = l R X a [ i ] \sum_{i=l}^{R} X \oplus a[i] 最大, \oplus 表示异或操作(不懂的请自行百度)。

输入描述

第一行一个整数 N N ,表示序列的长度。
第二行 N N 个整数,表示序列内的元素。
第三行一个整数 q q ,表示询问的个数。
接下来 q q 行,每行两个整数 [ L , R ] [L, R] ,表示询问的区间。

输出描述

输出 q q 行,每行一个整数表示答案。
若有多组可行解,请输出较小的解。

实例1

输入

5
4 78 12 1 3
3
2 5
1 4
3 3

输出

2147483632
2147483635
2147483635

备注

对于 30 % 30\% 的数据, n , q 10 n,q ≤ 10
对于 60 % 60\% 的数据, n , q 1000 n,q ≤ 1000
对于 100 % 100\% 的数据, n , q 1 0 5 n,q ≤ 10^5
保证 a i &lt; 2 31 a_i &lt; 2^{31}

题解

**正解:**类似于前缀和维护一下。

实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
	return f * x;
}

int a[maxn], cnt[35][maxn], n, q;

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		for(int j = 0; j < 31; j++) {
			cnt[j][i] = cnt[j][i-1] + ((a[i] >> j) & 1);//提取出数列中数字的每一位,加到
		}
	}
	scanf("%d", &q);
	while(q--) {
		int l = read(), r = read(), ans = 0;
		for(int j = 0; j < 31; j++) {
			int tmp = cnt[j][r] - cnt[j][l-1], len = r - l + 1;
			if(tmp < len - tmp) ans += 1 << j;
		}
		cout << ans << endl;
	}
	return 0; 
}

C C 硬币游戏

链接:https://www.nowcoder.com/acm/contest/189/C
来源:牛客网

题目描述

猜你喜欢

转载自blog.csdn.net/weixin_42619451/article/details/82820279