数字权重
链接:https://www.nowcoder.com/acm/contest/189/A
来源:牛客网
题目描述
小a有一个n位的数字,但是它忘了各个位上的数是什么,现在请你来确定各个位上的数字,满足以下条件:
设第
位的数为
,其中
为最高位,
为最低位,
为给定的数字。
- 不含前导
-
请你求出满足条件的方案数。
输入描述
两个整数
。
若存在无解的情况,请输出
。
输出描述
一个整数表示答案,对 取模。
实例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
备注
对于
的数据:
。
对于
的数据:
。
对于
的数据:
。
保证
。
题解
**正解:**我们化简条件
的式子后发现,只要让最低为比最高位大
就是可行解。所以我们枚举最高位和最低位。可行解与中间数字的大小无关,所以用快速乘求出中间数字的方案数即可。注意会爆 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;
}
毒瘤
链接:https://www.nowcoder.com/acm/contest/189/B
来源:牛客网
题目描述
小a有 个数 ,给出 个询问,每次询问给出区间 ,现在请你找到一个数 ,使得
- 。
- 最大, 表示异或操作(不懂的请自行百度)。
输入描述
第一行一个整数
,表示序列的长度。
第二行
个整数,表示序列内的元素。
第三行一个整数
,表示询问的个数。
接下来
行,每行两个整数
,表示询问的区间。
输出描述
输出
行,每行一个整数表示答案。
若有多组可行解,请输出较小的解。
实例1
输入
5
4 78 12 1 3
3
2 5
1 4
3 3
输出
2147483632
2147483635
2147483635
备注
对于
的数据,
。
对于
的数据,
。
对于
的数据,
。
保证
。
题解
**正解:**类似于前缀和维护一下。
实现
#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;
}
硬币游戏
链接:https://www.nowcoder.com/acm/contest/189/C
来源:牛客网