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

A A A 数字权重

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

题目描述

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

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

输入描述

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

输出描述

一个整数表示答案,对 1 0 9 + 7 10^9+7 109+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\% 30% 的数据: n , ∣ k ∣ = 5 n, |k| = 5 n,k=5
对于 60 % 60\% 60% 的数据: n , ∣ k ∣ ≤ 1000 n, |k| ≤ 1000 n,k1000
对于 100 % 100\% 100% 的数据: n , ∣ k ∣ ≤ 1 0 1 3 n, |k| ≤ 10^13 n,k1013
保证 n > 1 n > 1 n>1

题解

**正解:**我们化简条件 2 2 2的式子后发现,只要让最低为比最高位大 K 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 B 毒瘤 X O R XOR XOR

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

题目描述

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

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

输入描述

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

输出描述

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

实例1

输入

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

输出

2147483632
2147483635
2147483635

备注

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

题解

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

实现

#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 C 硬币游戏

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

题目描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值