题解:CF1275C #define Задача B ...
题解 CF1275C #define Задача B
题目大意
给定递归宏定义:
-
A_0(x) = x -
A_k(x) = A_{k-1}(x) A_{k-1}(x+1) A_{k-1}(x+3) A_{k-1}(x+4)
数组
解题思路
将下标转换为四进制,从低位到高位:
- 当前位为
0 :不加 - 当前位为
1 :+1 - 当前位为
2 :+3 - 当前位为
3 :+4
所有位的和即为答案。
代码实现
#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
long long n; cin >> n;
int ans = 0;
while (n) {
int d = n & 3; // 取四进制最低位
if (d == 1) ans += 1;
if (d == 2) ans += 3;
if (d == 3) ans += 4;
n >>= 2; // 右移两位,处理下一位
}
cout << ans << endl;
}
return 0;
}
复杂度
- 时间复杂度:
O(\log_4 n) 。 - 空间复杂度:
O(1) 。
本题解使用了 deepseek-v4-pro 进行润色以保证格式的正确。