当前位置:   article > 正文

【蓝桥杯】第十届软件类省赛(C/C++大学A组)超详细全题解_蓝桥杯历年真题解析c/c++大学a组

蓝桥杯历年真题解析c/c++大学a组

目录

试题 A:平方和(5分)

试题 B:数列求值(5分)

试题 C:最大降雨量(10分)

试题 D:迷宫(10分)

试题 E:RSA 解密(15分)

试题 F:完全二叉树的权值(15分)

试题 G:外卖店优先级(20分)

试题 H:修改数组(20分)

试题 I:糖果(25分)

试题 J:组合数问题(25分)


试题 A:平方和(5分)

题目链接:平方和

知识点:枚举、数位判断

解题思路:枚举 1 ~ 2019 每个数判断是否出现 2、0、1、9 这四个数字,只要出现至少一个,就将该数平方加入答案。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. bool check(int x){
  13. if(x == 2 || x == 0 || x == 1 || x == 9) return true;
  14. return false;
  15. }
  16. int main(){
  17. //freopen("input.txt", "r", stdin);
  18. ios::sync_with_stdio(false);
  19. cin.tie(NULL);
  20. cout.tie(NULL);
  21. ll sum = 0;
  22. for(int i = 1; i <= 2019; i++){
  23. int temp = i;
  24. while(temp){
  25. if(check(temp % 10)){
  26. sum += i * i;
  27. break;
  28. }
  29. temp /= 10;
  30. }
  31. }
  32. cout << sum << endl;
  33. return 0;
  34. }

最终答案:2658417853

试题 B:数列求值(5分)

题目链接:数列求值

知识点:递推、取模

解题思路:按照题目递推关系一步一步推即可,可以直接用数组推,也可以像下面代码一样用四个变量推。由于只需要求低四位,所以在推的过程中最多只需要存四位数字。怎么存呢?很简单,只需要每次将前三项之和对 10000 取模就好了。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. int main(){
  13. //freopen("input.txt", "r", stdin);
  14. ios::sync_with_stdio(false);
  15. cin.tie(NULL);
  16. cout.tie(NULL);
  17. int a = 1, b = 1, c = 1, temp;
  18. for(int i = 4; i <= 20190324; i++){
  19. temp = (a + b + c) % 10000;
  20. a = b;
  21. b = c;
  22. c = temp;
  23. }
  24. cout << c << endl;
  25. return 0;
  26. }

最终答案:4659

试题 C:最大降雨量(10分)

题目链接: 最大降雨量

知识点:思维、数学

解题思路:题目要求的是 7 周能量的中位数,每周能量又是一周中每天能量的中位数,为了使答案尽可能大,那么法术使用的顺序一定是按下面代码中的样子进行排列的,因此比答案值大的数只有 15 个,具体见代码中的分析。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. int main(){
  13. //freopen("input.txt", "r", stdin);
  14. ios::sync_with_stdio(false);
  15. cin.tie(NULL);
  16. cout.tie(NULL);
  17. /*
  18. a11 a12 a13 a14 a15 a16 a17
  19. a21 a22 a23 a24 a25 a26 a27
  20. a31 a32 a33 a34 a35 a36 a37
  21. a41 a42 a43 (a44) [a45] [a46] [a47]
  22. a51 a52 a53 [a54] [a55] [a56] [a57]
  23. a61 a62 a63 [a64] [a65] [a66] [a67]
  24. a71 a72 a73 [a74] [a75] [a76] [a77]
  25. */
  26. // '()'为最终答案的位置,'[]'为比答案大的数
  27. // 因此最终答案为49 - 15 = 34
  28. cout << 34 << endl;
  29. return 0;
  30. }

最终答案:34

试题 D:迷宫(10分)

题目链接:迷宫

知识点:最短路、bfs

解题思路:bfs 求最先到达每个点时上一步走的方向(0 表示下,1 表示左,2 表示右,3 表示上)存入 last 数组中,然后从终点退回起点,将每一步退到的点的 last 数组中的值转为 D、L、R、U 存入ans 数组,逆向输出就是答案。(由于只存第一次到达的情况,因此这样路径一定最短;又由于按 D、L、R、U 的顺序更新去到的点,因此这样得到的答案一定是字典序最小的)

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. const int N = 30 + 5, M = 50 + 5;
  13. char g[N][M], ans[N * M];
  14. bool st[N][M];
  15. int dx[4] = {1, 0, 0, -1};
  16. int dy[4] = {0, -1, 1, 0};
  17. char dir[4] = {'D', 'L', 'R', 'U'};
  18. int last[N][M];
  19. int n = 30, m = 50, cnt;
  20. void bfs(){
  21. queue<PII> q;
  22. q.push({1, 1});
  23. st[1][1] = true;
  24. while(q.size()){
  25. int x = q.front().first, y = q.front().second;
  26. q.pop();
  27. for(int i = 0; i < 4; i++){
  28. int xi = x + dx[i];
  29. int yi = y + dy[i];
  30. if(xi < 1 || xi > n || yi < 1 || yi > m || g[xi][yi] == '1' || st[xi][yi]) continue;
  31. q.push({xi, yi});
  32. st[xi][yi] = true;
  33. last[xi][yi] = i;
  34. }
  35. }
  36. int x = n, y = m;
  37. while(x != 1 || y != 1){
  38. int la = last[x][y];
  39. ans[cnt++] = dir[last[x][y]];
  40. x -= dx[la];
  41. y -= dy[la];
  42. }
  43. }
  44. int main(){
  45. //freopen("input.txt", "r", stdin);
  46. ios::sync_with_stdio(false);
  47. cin.tie(NULL);
  48. cout.tie(NULL);
  49. for(int i = 1; i <= n; i++){
  50. for(int j = 1; j <= m; j++){
  51. cin >> g[i][j];
  52. }
  53. }
  54. bfs();
  55. for(int i = cnt - 1; i >= 0; i--){
  56. cout << ans[i];
  57. }
  58. return 0;
  59. }

最终答案:DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDR

试题 E:RSA 解密(15分)

题目链接:RSA 解密

知识点:分解质因数、扩展欧几里得、快速幂、快速乘

解题思路:

根据问题描述,n = p q 且 p,q 为质数,那么可以先将 p 和 q 算出来,计算代码如下注释部分。得到 p = 891234941,q = 1123984201;

然后根据 de,可以得到同余方程 de \equiv 1 (mod \;(p - 1)(q - 1)),其中 e 是未知数。这就可以借助扩展欧几里得求出这个同余方程的解,得到 e = 823816093931522017;

最后用快速幂求 C^e \; mod \; n,由于数据过大,快速幂中的乘法需要使用快速乘进行计算。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. ll quick_mul(ll a, ll b, ll m){
  13. ll res = 0;
  14. while(b){
  15. if(b & 1) res = (res + a) % m;
  16. a = (a + a) % m;
  17. b >>= 1;
  18. }
  19. return res;
  20. }
  21. ll quick_pow(ll a, ll b, ll m){
  22. ll res = 1;
  23. while(b){
  24. if(b & 1) res = quick_mul(res, a, m) % m;
  25. a = quick_mul(a, a, m) % m;
  26. b >>= 1;
  27. }
  28. return res;
  29. }
  30. ll ex_gcd(ll a, ll b, ll &x, ll &y){
  31. if(!b){
  32. x = 1, y = 0;
  33. return a;
  34. }
  35. ll d = ex_gcd(b, a % b, y, x);
  36. y -= a / b * x;
  37. return d;
  38. }
  39. ll cal(ll d, ll m){
  40. ll x, y;
  41. ex_gcd(d, m, x, y);
  42. return (x % m + m) % m;
  43. }
  44. int main(){
  45. //freopen("input.txt", "r", stdin);
  46. ios::sync_with_stdio(false);
  47. cin.tie(NULL);
  48. cout.tie(NULL);
  49. ll n = 1001733993063167141, p, q, d = 212353, e, C = 20190324, X;
  50. //for(int i = 2; i * i <= n; i++){
  51. // if(n % i == 0){
  52. // p = i;
  53. // q = n / i;
  54. // break;
  55. // }
  56. //}
  57. p = 891234941, q = 1123984201;
  58. e = cal(d, (p - 1) * (q - 1));
  59. X = quick_pow(C, e, n);
  60. cout << X << endl;
  61. return 0;
  62. }

最终答案:579706994112328949

试题 F:完全二叉树的权值(15分)

题目链接:完全二叉树的权值

知识点:树形结构

解题思路:由于是完全二叉树,第 i 层的开始结点为 2^{i - 1},根据这个性质,计算每层的权值和,然后遍历一遍找到最大权值和,将层数输出即可。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. const int N = 100005;
  13. int a[N], sum[N];
  14. int main(){
  15. //freopen("input.txt", "r", stdin);
  16. ios::sync_with_stdio(false);
  17. cin.tie(NULL);
  18. cout.tie(NULL);
  19. int n;
  20. cin >> n;
  21. int cnt = 1;
  22. for(int i = 1; i <= n; i++){
  23. if(i == (1 << cnt)) cnt++;
  24. cin >> a[i];
  25. sum[cnt] += a[i];
  26. }
  27. int ma = -INF, ans = 0;
  28. for(int i = 1; i <= cnt; i++){
  29. if(sum[i] > ma){
  30. ma = sum[i];
  31. ans = i;
  32. }
  33. }
  34. cout << ans << endl;
  35. return 0;
  36. }

试题 G:外卖店优先级(20分)

题目链接:外卖店优先级

知识点:思维、模拟

解题思路:将订单按时间进行排序,遍历所有订单,每个订单的这家店当前的优先级需要减去当前时间到这家店上一次收到订单的时间之差(最多减至 0),然后记录这家店是否在优先缓存中。最后再遍历一遍每家店,如果这家店最后一次收到订单不是在 T 时刻,就需要更新一下这家店的优先级,并判断是否在优先缓存中,如果在,就将答案 +1。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. const int N = 1000005;
  13. int prio[N], last[N];
  14. bool st[N];
  15. struct bill{
  16. int ts, id;
  17. bool operator < (const bill &u) const{
  18. return ts < u.ts;
  19. }
  20. }b[N];
  21. int main(){
  22. //freopen("input.txt", "r", stdin);
  23. ios::sync_with_stdio(false);
  24. cin.tie(NULL);
  25. cout.tie(NULL);
  26. int n, m, t;
  27. cin >> n >> m >> t;
  28. for(int i = 1; i <= m; i++){
  29. cin >> b[i].ts >> b[i].id;
  30. }
  31. sort(b + 1, b + m + 1);
  32. for(int i = 1; i <= m; i++){
  33. int ts = b[i].ts, id = b[i].id;
  34. if(ts != last[id]){
  35. prio[id] = max(0, prio[id] - (ts - last[id] - 1));
  36. }
  37. if(prio[id] <= 3) st[id] = false;
  38. prio[id] += 2;
  39. if(prio[id] > 5) st[id] = true;
  40. last[id] = ts;
  41. }
  42. int ans = 0;
  43. for(int i = 1; i <= n; i++){
  44. if(last[i] < t){
  45. prio[i] -= t - last[i];
  46. if(prio[i] <= 3) st[i] = false;
  47. }
  48. if(st[i]) ans++;
  49. }
  50. cout << ans << endl;
  51. return 0;
  52. }

试题 H:修改数组(20分)

题目链接:修改数组

知识点:并查集

解题思路:由于数据范围较大,正常遍历和记录一个数是否存在的方法就不好用了。这就需要用到一种特殊的数据结构——单链表式的并查集。并查集的根结点表示大于等于当前这个数并且在之前没出现过的数;查询过程同时更新每个点的根结点,也就是一般并查集的 find() 函数。

举个例子:2 1 1 3 4,一开始先预处理 fa[i] = i。

输入第一个数 2,由于 fa[2] = 2,说明这个数前面没有出现过,不变直接输出 2,让 fa[2] = 3,(这样下次如果遇到 2 这个数,就直接置为 3 输出);

接着输入 1,由于fa[1] = 1,不变直接输出 1,让 fa[1] = 2;

接着输入 1,由于 fa[1] = 2,fa[2] = 3,fa[3] = 3,fa[1] 就更新为 3,输出 3,让 fa[3] = 4;

接着输入 3,由于 fa[3] = 4,fa[4] = 4,输出 4,让 fa[4] = 5;

最后输入 4,由于 fa[4] = 5,fa[5] = 5,输出 5,让 fa[5] = 6。

这样最终就输出了 2 1 3 4 5。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. const int N = 1000005;
  13. int a[N], fa[N];
  14. int find(int x){
  15. if(fa[x] != x) fa[x] = find(fa[x]);
  16. return fa[x];
  17. }
  18. int main(){
  19. //freopen("input.txt", "r", stdin);
  20. ios::sync_with_stdio(false);
  21. cin.tie(NULL);
  22. cout.tie(NULL);
  23. int n;
  24. cin >> n;
  25. for(int i = 1; i < N; i++){
  26. fa[i] = i;
  27. }
  28. for(int i = 1; i <= n; i++){
  29. int x;
  30. cin >> x;
  31. x = find(x);
  32. cout << x << ' ';
  33. fa[x] = x + 1;
  34. }
  35. cout << endl;
  36. return 0;
  37. }

试题 I:糖果(25分)

题目链接:糖果

 

知识点:二进制、状压dp

解题思路:可以看得出来,这题的思路很可能是 dfs 或者状压dp(看数据范围做题)。考虑每包糖果用一个 0 ~ 2^{m} - 1 的 m 位二进制表示,第 i 位是 1 表示有第 i - 1 种口味的糖果。这样样例中的 6 包糖果就可以表示为 00011、00111、00101、10110、11010、10011。这样就可以用状压dp进行解题了。令 dp[i][j] 表示考虑到第 i 包糖果,已经得到的口味种类的二进制表示为 j。那么可以得到状态转移方程:dp[i][j \; | \; a[i]] = min(dp[i][j \; | \; a[i]], dp[i - 1][j] + 1)

然后考虑初始化,由于求的最小值,将 dp 数组初始化为大于等于 n + 1 的数(因为能够吃到所有种类的糖果的最坏情况是将 n 包全部买下),然后由于每包糖果都有不同种类的糖果,初始化 dp[i][a[i]] = 1。

由于数据较大,数组需要开到 100 * 2^{20} 的大小(数量级达到了 10^8),这是做不了的,因此就需要进行空间优化。类似于背包问题,将 dp 数组优化成一维,遍历二进制时从后往前,这样就解决了空间问题。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. const int N = 105;
  13. int a[N], dp[1 << 20];
  14. int main(){
  15. freopen("input.txt", "r", stdin);
  16. ios::sync_with_stdio(false);
  17. cin.tie(NULL);
  18. cout.tie(NULL);
  19. int n, m, k;
  20. cin >> n >> m >> k;
  21. for(int i = 1; i <= n; i++){
  22. for(int j = 1; j <= k; j++){
  23. int x;
  24. cin >> x;
  25. a[i] |= 1 << (x - 1);
  26. }
  27. }
  28. memset(dp, 0x3f, sizeof dp);
  29. for(int i = 1; i <= n; i++){
  30. dp[a[i]] = 1;
  31. for(int j = (1 << m) - 1; j >= 0; j--){
  32. dp[j | a[i]] = min(dp[j | a[i]], dp[j] + 1);
  33. }
  34. }
  35. if(dp[(1 << m) - 1] == INF) cout << -1 << endl;
  36. else cout << dp[(1 << m) - 1] << endl;
  37. return 0;
  38. }

试题 J:组合数问题(25分)

题目链接:组合数问题

知识点:组合数、Lucas定理、数位dp

解题思路:C_i^j \equiv 0 (mod \; k) 即求 Cij,由于 i 和 j 会达到 10^{18} 的级别,求组合数就需要考虑到 Lucas 定理了。

(Lucas 定理:Cnm

假设 a = 69,b = 47,k = 5,那么 C6947

将 a 和 b 用 k 进制表示一下,可以发现 a = 69_{(10)} = 234_{(5)}b = 47_{(10)} = 142_{(5)}。这恰好和上面分解后的组合数对应。

再根据组合数 C_n^m 的性质,当 n < m 的时候,C_n^m = 0,那么就只需要分析 k 进制下的 a 和 b,如果 a 的某一位 p 小于 b 的该位 q,那么式子中一定会出现一个 C_p^q,则 Cab

这样就将原题转换成分析数位的问题了,这就可以用数位dp来解决。

但是仔细考虑后会发现如果只找某一位的关系是非常复杂的,因此可以将其转换为总数减去 i 的每一位都大于等于 j 的数量。

这样数位dp就比较好考虑了:

令 dp[i][0] 表示:a 的前 i 位未全取到上限值,b 的前 i 位也未全取到上限值;

dp[i][1] 表示:a 的前 i 位全取到上限值,b 的前 i 位未全取到上限值;

dp[i][2] 表示:a 的前 i 位未全取到上限值,b 的前 i 位全取到上限值;

dp[i][3] 表示:a 的前 i 位全取到上限值,b 的前 i 位也全取到上限值。

令 cal1(x, y) 表示:x 取值 [0, x],y 取值 [0, y] 时 x ≥ y 的数量;

cal2(x, y) 表示:x 取定值 x,y 取值 [0, y] 时 x ≥ y 的数量;

cal3(x, y) 表示:x 取值 [0, x],y 取定值 y 时 x ≥ y 的数量。

考虑状态转移:

dp[i][0] = dp[i + 1][0] * cal1(k - 1, k - 1) + dp[i + 1][1] * cal1(a[i] - 1, k - 1) + dp[i + 1][2] * cal1(k - 1, b[i] - 1) + dp[i + 1][3] * cal1(a[i] - 1, b[i] - 1)

dp[i][1] = dp[i + 1][1] * cal2(a[i], k - 1) + dp[i + 1][3] * cal2(a[i], b[i] - 1)

dp[i][2] = dp[i + 1][2] * cal3(k - 1, b[i]) + dp[i + 1][3] * cal3(a[i] - 1, b[i])

dp[i][3] = dp[i + 1][3] \; \& \; (a[i] \geqslant b[i])

分析一下这四个方程:

1.dp[i][0]:前 i 位 a 和 b 都没全取到上限值,

(1)可以由前 i + 1 位 a 和 b 都没全取到上限值转移,此时 a 和 b 的第 i 位均可以取 0 ~ k - 1;

(2)也可以由前 i + 1 位 a 全取到上限值,b没全取到上限值转移,此时 a 的第 i 位只能取 0 ~ a[i] - 1,b 的第 i 位可以取 0 ~ k - 1;

(3)也可以由前 i + 1 位 a 没全取到上限值,b 全取到上限值转移,此时 a 的第 i 位可以取 0 ~ k - 1,b 的第 i 位只能取 0 ~ b[i] - 1;

(4)还可以由前 i + 1 位 a 和 b 全取到上限值转移,此时 a 的第 i 位只能取 0 ~ a[i] - 1,b 的第 i 位只能取到 0 ~ b[i] - 1。

2.dp[i][1]:前 i 位 a 全取到上限值,b 没全取到上限值,

(1)可以由前 i + 1 位 a 全取到上限值,b 没全取到上限值转移,此时 a 的第 i 位只能取 a[i],b 的第 i 位能取 0 ~ k - 1;

(2)也可以由前 i + 1 位 a 和 b 全取到上限值转移,此时 a 的第 i 位只能取 a[i],b 的第 i 位只能取 0 ~ b[i] - 1。

3.dp[i][2]:前 i 位 a 没全取到上限值,b 全取到上限值,

(1)可以由前 i + 1 位 a 没全取到上限值,b 全取到上限值转移,此时 a 的第 i 位能取 0 ~ k - 1,b 的第 i 位只能取 b[i];

(2)也可以由前 i + 1 位 a 和 b 全取到上限值转移,此时 a 的第 i 位只能取 0 ~ a[i] - 1,b 的第 i 位只能取 b[i]。

4.dp[i][3]:前 i 位 a 和 b 都全取到上限值,只能由前 i + 1 位 a 和 b 都全取到上限值转移,此时 a 的第 i 位只能取 a[i],b 的第 i 位只能取 b[i],如果 a[i] >= b[i],那么个数为 1,反之个数为 0。

最后解释一下 cal1()、cal2()、cal3() 的计算过程:

1.cal1():由于 x 能取 [0, x],y 能取 [0, y],那么要求 x ≥ y 的个数,有两种情况:

(1)x < y:当 x 取 0 时,y 能取 [0, 0],x 取 1 时,y 能取 [0, 1],……,x 取 x 时,y 能取 [0, x],对这些取值个数求和,即 1 + 2 + ... + (x + 1),也就是 (x + 2)(x + 1) / 2;

(2)x ≥ y:当 x 取 0 时,y 能取 [0, 0],x 取 1 时,y 能取 [0, 1],……,x 取 y 时,y 能取 [0, y],x 取 y + 1 时,y 还是只能取 [0, y]……,对这些取值个数求和,即 1 + 2 + ... + (y + 1) + (x - y)(y + 1),也就是 (y + 2)(y + 1) / 2 + (x - y)(y + 1);

2.cal2():由于 x 取定值 x,y 能取 [0, y],那么要求 x ≥ y 的个数,有两种情况:

(1)x < y:y 只能取 [0, x];

(2)x ≥ y:y 能取 [0, y]。

整合一下,总个数为 min(x, y) + 1;

3.cal3():由于 x 能取 [0, x],y 取定值 y,那么要求 x ≥ y 的个数,有两种情况:

(1)x < y:无论 x 怎么取,均是 x < y,个数为 0;

(2)x ≥ y:x 只能取 [y, x],个数为 x - y + 1。

AC代码:

  1. #include <bits/stdc++.h>
  2. #define lowbit(x) (x & -x)
  3. #define mid (l + r >> 1)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef pair<int, int> PII;
  8. typedef pair<ll, ll> PLL;
  9. const int INF = 0x3f3f3f3f;
  10. const ll LLF = 0x3f3f3f3f3f3f3f3f;
  11. const int mod = 1000000007;
  12. const int inv2 = 500000004;
  13. const int N = 105;
  14. int a[N], b[N];
  15. ll dp[N][4];
  16. ll cal1(ll x, ll y){
  17. if(x < 0 || y < 0) return 0;
  18. ll xx = x, yy = y;
  19. x %= mod; y %= mod;
  20. if(xx < yy) return (x + 2) * (x + 1) % mod * inv2 % mod;
  21. return ((y + 2) * (y + 1) % mod * inv2 % mod + (x - y) * (y + 1) % mod) % mod;
  22. }
  23. ll cal2(ll x, ll y){
  24. return min(x, y) + 1;
  25. }
  26. ll cal3(ll x, ll y){
  27. return x < y ? 0 : x - y + 1;
  28. }
  29. int main(){
  30. //freopen("input.txt", "r", stdin);
  31. ios::sync_with_stdio(false);
  32. cin.tie(NULL);
  33. cout.tie(NULL);
  34. int t, k;
  35. cin >> t >> k;
  36. while(t--){
  37. memset(dp, 0, sizeof dp);
  38. memset(a, 0, sizeof a);
  39. memset(b, 0, sizeof b);
  40. ll n, m;
  41. cin >> n >> m;
  42. if(m > n) m = n;
  43. ll ans = cal1(n, m);
  44. int alen = 0, blen = 0;
  45. while(n){
  46. a[alen++] = n % k;
  47. n /= k;
  48. }
  49. while(m){
  50. b[blen++] = m % k;
  51. m /= k;
  52. }
  53. dp[alen][3] = 1;
  54. for(int i = alen - 1; i >= 0; i--){
  55. dp[i][0] = (dp[i + 1][0] * cal1(k - 1, k - 1) + dp[i + 1][1] * cal1(a[i] - 1, k - 1) + dp[i + 1][2] * cal1(k - 1, b[i] - 1) + dp[i + 1][3] * cal1(a[i] - 1, b[i] - 1)) % mod;
  56. dp[i][1] = (dp[i + 1][1] * cal2(a[i], k - 1) + dp[i + 1][3] * cal2(a[i], b[i] - 1)) % mod;
  57. dp[i][2] = (dp[i + 1][2] * cal3(k - 1, b[i]) + dp[i + 1][3] * cal3(a[i] - 1, b[i])) % mod;
  58. dp[i][3] = dp[i + 1][3] & (a[i] >= b[i]);
  59. }
  60. ans -= dp[0][0] + dp[0][1] + dp[0][2] + dp[0][3];
  61. ans = (ans % mod + mod) % mod;
  62. cout << ans << endl;
  63. }
  64. return 0;
  65. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/48800
推荐阅读
相关标签
  

闽ICP备14008679号