Skip to main content
  1. Posts/

cf 611 B ||codeforces goodbye 2015 B. New Year and Old Property (数学或者数位dp)

Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/611/problem/B 题意:问a到b(1E18),二进制表示中只有一个0的数有多少个。 思路:这么大的数。。。不是有循环节就是math problems.    UD:20160318讲道理还有可能是数位dp好不好。。。 我们发现可以很容易得算出1到x的二进制表示中只有一个0 的数有多少个。

problem solved.

20160318update:学了数位dp后又看到这题。。。这题显然是数位dp啊。。。亏我找规律搞了出来2333.

后面附上数位dp方法AC的代码

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月30日 星期三 22时49分02秒
 4File Name :code/cf/goodbye2015/B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const int N=1E4+7;
30LL a,b;
31LL p[N];
32LL c[N];
33LL cal( LL x)
34{
35    return ((x-1LL)*x)/2LL;
36}
37LL solve (LL x)
38{
39    if (x==0LL) return 0;
40    LL res= 0LL;
41    LL cnt = 0LL;
42    LL xx = x;
43    while (xx)
44    {
45	cnt++;
46	p[cnt] = xx%2LL;
47	xx/=2LL;
48    }
49    ms(c,0);
50    res+=cal(cnt-1LL);
51    LL tmp = (1LL<<cnt)-1LL;
52    for ( LL i = 0 ; i <cnt-1 ; i++)
53    {
54	LL happ = 1LL<<i;
55	c[i]=tmp-happ;
56    }
57
58    sort(c,c+cnt-1);
59
60    for ( LL i = 0  ; i< cnt -1 ; i++)
61    {
62	if (x>=c[i]) res++;
63    }
64
65
66    return res;
67}
68int main()
69{
70
71	cin>>a>>b;
72	LL ans = solve(b)-solve(a-1LL);
73	cout<<ans<<endl;
74
75  #ifndef ONLINE_JUDGE
76  fclose(stdin);
77  #endif
78    return 0;
79}

数位dp的方法:

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年03月18日 星期五 16时33分04秒
 4File Name :code/cf/problem/611B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33LL l,r;
34int digit[80];
35LL dp[80][80];
36
37
38LL dfs( int pos,int cnt,bool limit,bool prehasnonzero)
39{
40    if (pos==0) return cnt==1;
41    if (!limit&&prehasnonzero&&dp[pos][cnt]!=-1) return dp[pos][cnt];
42
43    int mx = limit?digit[pos]:1;
44    LL res = 0LL ;
45    if (prehasnonzero)
46    {
47	for ( int i =  0 ; i <= mx ; i++)
48	{
49	    res+=dfs(pos-1,i==0?cnt+1:cnt,limit&&i==mx,true);
50	}
51    }
52    else
53    {
54	for ( int i = 0 ; i <= mx; i++)
55	{
56	    res+=dfs(pos-1,0,limit&&i==mx,i==0?false:true);
57	}
58    }
59
60    if (!limit&&prehasnonzero) dp[pos][cnt] = res;
61    return res;
62}
63LL solve  ( LL n)
64{
65    int len =  0 ;
66    ms(digit,0);
67
68    while (n)
69    {
70	digit[++len] = n % 2;
71	n /= 2;
72    }
73    return dfs(len,0,true,false);
74}
75int main()
76{
77	#ifndef  ONLINE_JUDGE
78	freopen("code/in.txt","r",stdin);
79  #endif
80	ms(dp,-1);
81	cin>>l>>r;
82	//cout<<"solve:"<<solve()<<endl;
83	LL ans = solve(r) - solve (l-1);
84	cout<<ans<<endl;
85
86  #ifndef ONLINE_JUDGE
87  fclose(stdin);
88  #endif
89    return 0;
90}

Related

hdu 2138 How many prime numbers

ACM STEPS里的…这题前面一道是求LCM….结果接下来就是这么一道。。。 朴素会超….筛法会爆….题目顺序真是按照难度来的? 于是想到 Miller-Rabin素数测试……. 这个方法是基于费马小定理 我的理解就是… 如果我要判断n是否为素数 只要取k个数 如果满足 a^(n-1)mod n =1 那么n就很可能为素数。 证明什么的…暂时还是算了吧…论文里貌似扯了一大堆 第一次用,竟然真的A了。。。。 感觉更好的办法也许是先打一个比较小的素数表,然后每次random选取若干个进行判断…那样应该更可靠些? 本来想WA掉之后再改的。。。没想到这么写就A掉了。。。。杭电数据略水?

codeforces 30 C. Shooting Gallery

·1 min
http://codeforces.com/contest/30/problem/C 题意:给出n个target在一个二维平面上。给出每个target的坐标,出现的时间,以及击中的概率。target出现之后就会瞬间消失,枪移动的单位速度为1,射击不需要时间。问能击中的target的最大期望是多少。

codeforces #327 A. Flipping Game

·1 min
http://codeforces.com/contest/327/problem/A 题意:给定一段序列,只由0,1组成。要求选一段非空区间,做翻转操作(0变1,1变0),问变完之后1最多能有多少。 思路:最后的1个个数=初始的1的个数+变换区间的0的个数-变换区间的1的个数。初始的是常数。那么我们只要找到某一个区间内,0的个数-1的个数有最大值即可。如果a[i]==0的时候令b[i]=1,否则b[i]=0,那就是经典了最大连续区间和的问题了。dp的思想o(n)可以解决。

codeforces 456 C. Boredom

·1 min
http://codeforces.com/contest/456/problem/C 题意:给出n(1E5)个数(1E5),每次可以选一个数a[k]并删掉a[k],a[k]-1,a[k]+1得到a[k]分,问最多能得到的分数。 思路:裸dp.f[i]表示选到数i的时候能达到的最大分数。开一个计数数组cnt[x]表示数字x出现的次数。那么显然有f[0]=0,f[1]=cnt[1],f[i(i>=2)] = max(f[i-1],f[i-2]+f[i]*cnt[i]);答案为f[max(a[i])],注意要开long long

codeforces #332 div 2 D. Spongebob and Squares

·2 mins
http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的时候gg了。。其实稍微在纸上推一下。就会得到对于n,m的矩形,一共会有-nnn+3nnm+n+3n*m的方格。数量级是n3。 我们可以实际跑一遍。发现对于x1E18的数量级,n不会超过1442550,1E6,可以搞。