Saruman’s Level Up~(多校赛算组合数)

本文介绍了一个基于二进制表示中1的数量为3的倍数的天数来预测萨鲁曼塔楼等级的问题。通过将给定的天数转换为二进制并计算符合条件的天数来确定塔楼的等级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Saruman’s army of orcs and other dark minions continuously mine and harvest lumber out of the land surrounding his mighty tower for N continuous days. On day number i, Saruman either chooses to spend resources on mining coal and harvesting more lumber, or on raising the level (i.e., height) of his tower. He levels up his tower by one unit only on days where the binary representation of i contains a total number of 1’s that is an exact multiple of 3. Assume that the initial level of his tower on day 0 is zero. For example, Saruman will level up his tower on day 7 (binary 111), next on day 11 (binary 1011) and then day 13, day 14, day 19, and so on. Saruman would like to forecast the level of his tower after N days. Can you write a program to help?

Input

The input file will contain multiple input test cases, each on a single line. Each test case consists of a positive integer N < 1016, as described above. The input ends on end of file.

Output

For each test case, output one line: “Day N: Level = L”, where N is the input N, and L is the number of levels after N days.

Sample Input

2
19
64

Sample Output

Day 2: Level = 0
Day 19: Level = 5
Day 64: Level = 21


这题其实就是一个模拟,其实把n转化为二进制,
然后求出小于等于n有多少个数的二进制中含有1的个数是3的倍数
就是计算组合数,思维题,
例如1000100 当计算到第一个1的时候 就是加上c(6,3)+c(6,6)
此时bits的个数加一,然后到第二个1的时候就是看看
(i+bits)%==0 0<=i<=2 时候存在 i 再加上组合数就是了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 typedef long long LL;
 6 LL gcd(LL a, LL b ) {
 7     if (b == 0) return a;
 8     return gcd(b, a % b);
 9 }
10 LL combine(LL n, LL r) {
11     LL ret = 1;
12     if (r > n - r) r = n - r;
13     for (LL i = 1 ; i <= r ; i++) {
14         LL k = n - i + 1;
15         LL g = gcd(i, k);
16         ret = ret / (i / g) * (k / g);
17     }
18     return ret;
19 }
20 LL ans(LL n) {
21     LL ret = 0, bits = 0, k = ((LL)1 << 60), r = 60;
22     while(k) {
23         if (k & n) {
24             for (int i = 0 ; i <= r ; i++) {
25                 if ((i + bits) > 0 && ((i + bits) % 3 == 0)) ret += combine(r, i);
26             }
27             bits++;
28         }
29         k = k >> 1;
30         r--;
31     }
32     if (bits > 0 && bits % 3 == 0) ret++;
33     return ret;
34 }
35 int main() {
36     LL n;
37     while(scanf("%lld", &n) != EOF) {
38         printf("Day %lld: Level = %lld\n", n, ans(n));
39     }
40     return 0;
41 }

 



转载于:https://www.cnblogs.com/qldabiaoge/p/8877867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值