codeforces Good Bye 2015 B. New Year and Old Property

题目链接:http://codeforces.com/problemset/problem/611/B

题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制数表示只有 1 个 0 的数量是多少。

  ***********************

  首先贴出一段绝对会超时的代码,连提交的勇气都木有呢~因为第四组数据已经接近龟速了,所以。。。(大家可忽略)

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6
 7 typedef __int64 ll;
 8 ll a, b;
 9 ll ans;
10
11 int main()
12 {
13     #ifndef ONLINE_JUDGE
14         freopen("in.txt", "r", stdin);
15     #endif // ONLINE_JUDGE
16
17     while (scanf("%I64d%I64d", &a, &b) != EOF) {
18         __int64 i;
19         ans = 0;
20
21         for (i = a; i <= b; i++) {
22            __int64 l = i;
23            int c = 0;
24            while (l && c <= 1) {
25                c += ((l & 1) == 0 ? 1 : 0);
26                l >>= 1;
27           }
28           if (c == 1)
29             ans++;
30
31         }
32         printf("%I64d\n", ans);
33     }
34     return 0;
35 }

  ***********************

  

  讲讲正确的解题思路 —— 就是暴力枚举啦.......当然不是将十进制数先化成二进制数再慢慢比对啦,我上面的代码就类似这个思想了,讲多都是泪,呜呜呜~~~

  是模拟二进制数,在 [1, 10^18] 范围内,统计出只出现二进制表示0只出现1次的数

  我还是第一次知道原来可以这样算= =、、、 慢慢积累经验吧~~~

  另外讲一讲数的范围, 2^61 已经比 10^18要大了,所以枚举的时候,举例到61次就行,代码有详尽的注释,大家慢慢欣赏 ^_^

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6
 7 typedef __int64 ll;
 8 const int bit = 61;  // 2^61 = 1,152,921,504,606,846,976
 9
10 int main()
11 {
12     #ifndef ONLINE_JUDGE
13         freopen("in.txt", "r", stdin);
14     #endif // ONLINE_JUDGE
15
16     ll a, b;
17     while (scanf("%I64d%I64d", &a, &b) != EOF) {
18         int ans = 0;
19
20         for (int i = 2; i <= bit; i++) {
21             ll mea = (1ll << i) - 1;   // 1, 11, 111, 1111, ...
22
23             for (int j = 0; j < i-1; j++) {
24                 ll t = mea - (1ll << j); // (1ll<<j): 1, 10, 100, 1000, ...
25                 ans += (t >= a && t <= b);
26             }
27         }
28         printf("%d\n", ans);
29     }
30     return 0;
31 }

  

  

时间: 2024-08-05 17:09:18

codeforces Good Bye 2015 B. New Year and Old Property的相关文章

Good Bye 2015 B. New Year and Old Property

题目链接:http://codeforces.com/problemset/problem/611/B 解题思路: 直接暴力推出所有符合条件的. 由进制转换可以知道,二进制只有1个0也就是十进制减去前面任意一个2的次方 然后脑残一样的用了位运算,死都无法表示64位,只能32位,还以为电脑出问题了..换了pow秒过... 实现代码: #include<bits/stdc++.h> using namespace std; #define ll long long int main() { ll

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

Codeforces VK Cup 2015 Wild Card Round 1 (AB)

比赛链接:http://codeforces.com/contest/522 A. Reposts time limit per test:1 second memory limit per test:256 megabytes One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started

Good Bye 2015 A

Problem A:http://codeforces.com/problemset/problem/611/A A. New Year and Days 题意:某人要在2016年收集糖果,有两种不同的收集方式,按week或month.x表示week的周x或月的x号,只有到每周或每月的x时才会收集一颗糖果,问:在给定的月或周条件内,2016年能收集多少颗糖果. 思路:我是用数组存好,其实也可以更简洁,周的话除了5,6,7是53个,其他都是52个:月的话1-29都是12(闰年),30是11,31是

Good Bye 2015 A. New Year and Days 签到

A. New Year and Days Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015. Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016. Lima

Codeforces Good Bye 2018 F

题目链接:https://codeforces.com/contest/1091/problem/F 首先考虑一个贪心的策略,我们在岩浆上飞,在水里游,在路上走,这样一路走到结尾.但我们发现途中会出现几种情况. 情况1: 我们遇到了岩浆,但是剩余的能量不够我们飞行过去,这时候我们要在前面通过原地转圈蓄能量.我们可以全局维护当前走过的路中是否有水,如果有,我们就以\(3\)代价原地走路,否则,只能以\(5\)代价原地走路. 情况2: 走到结尾的时候,我们有剩余的能量,这时候我们要通过把走路和游泳换