【枚举】Southwestern Europe Regional Contest H - Sheldon Numbers

https://vjudge.net/contest/174235#problem/H

【题意】

  • 求[x,y]之间有多少个Sheldon Number
  • Sheldon Number是二进制满足以下条件的数:
  • 可以写成ABABAB . . . ABA或ABABAB . . . AB,其中,A是连续的n个1,B是连续的m个0
  • 这个二进制数可以没有0但不能没有1

【思路】

  • 对于确定的n,m,和二进制数的长度len,二进制数唯一确定的,所以按n,m,len枚举的话复杂度为64^3
  • 我们可以预处理所有的Sheldon Number,保存在数组中,排序
  • 可能有重复,所以要去重
  • 对所求的[x,y],在数组中二分查找x和y,x为lower_bound,y为upper_bound-1

【Accepted】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 #include<cmath>
 7 
 8 using namespace std;
 9 typedef unsigned long long ull;
10 typedef long long ll;
11 ll x,y;
12 ll two[63];
13 ll ans[262150];
14 void init()
15 {
16     two[0]=1ll;
17     for(int i=1;i<=63;i++)
18     {
19         two[i]=two[i-1]*2;
20     }
21 }
22 ll calc(int n,int m,int len)
23 {
24     int digit[64];
25     int cntn=0,cntm=0;
26     for(int i=0;i<len;i++)
27     {
28         if(cntn<n)
29         {
30             digit[i]=1;
31             cntn++;
32             if(cntn==n)
33             {
34                 cntm=0;
35             }
36         }
37         else
38         {
39             digit[i]=0;
40             cntm++;
41             if(cntm==m)
42             {
43                 cntn=0;
44             }
45         }
46     }
47     ll res=0ll;
48     for(int i=0;i<len;i++)
49     {
50         res+=1ll*digit[i]*two[len-1-i];
51     }
52     return res;
53     
54 }
55 int main() 
56 {
57     init();
58     int cnt=0;
59         for(int i=1;i<=63;i++)
60         {
61             for(int j=0;j<=63;j++)
62             {
63                 for(int k=1;k<=63;k++)
64                 {
65                     if((j==0&&k%i==0&&k/i>=1)||(j!=0&&k%(i+j)==0&&k/(i+j)>=1)||(j!=0&&(k+j)%(i+j)==0&&(k+j)/(i+j)>=2))
66                     {
67                         ans[cnt++]=calc(i,j,k);    
68                     }        
69                 }        
70             }        
71         }
72         sort(ans,ans+cnt);
73         cnt=unique(ans,ans+cnt)-ans;
74         while(~scanf("%lld%lld",&x,&y))
75         {
76             int p=lower_bound(ans,ans+cnt,x)-ans;
77             int q=upper_bound(ans,ans+cnt,y)-ans-1;
78             cout<<q-p+1<<endl;        
79         }
80         return 0;
81 }
View Code

 

转载于:https://www.cnblogs.com/itcsl/p/7252913.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Certainly, I can write a small essay of 1000 words about Yunnan province of China. Yunnan is one of the most beautiful provinces in China, located in the southwestern part of the country. It is surrounded by mountains and is known for its diverse topography and natural beauty. The province is home to 25 different ethnic minority groups, which gives it a rich cultural heritage that is unique to the region. Yunnan has a rich history, and there are many fascinating historical sites to visit throughout the province. One of the most famous historical sites is the ancient city of Dali, which dates back to the 14th century. The city is surrounded by the beautiful Cangshan Mountains and is known for its traditional architecture and unique culture. Another must-visit city in Yunnan is Lijiang, which is a UNESCO World Heritage Site. The old town of Lijiang is well-preserved and is a beautiful example of traditional Naxi architecture. The city is also famous for its Jade Dragon Snow Mountain, which is one of the most magnificent peaks in the world. In addition, Yunnan has some of the most beautiful natural landscapes in China, with towering mountains, deep gorges, and clear lakes. The Three Parallel Rivers of Yunnan Protected Areas is a UNESCO World Heritage Site that encompasses the area where the Jinsha, Lancang, and Nujiang rivers flow parallel to each other for over 300 kilometers. This region features spectacular natural scenery, including the deep canyons, majestic waterfalls, and rare species of animals and plants. Yunnan is also known for its vibrant and diverse cuisine. The province’s cuisine is influenced by the region’s ethnic minority groups, resulting in a unique blend of flavors and cooking styles. One of the most famous dishes in Yunnan is the ‘Crossing the Bridge Noodles,’ which is a rice noodle soup that is served with a variety of meats, vegetables, and seasonings. In conclusion, Yunnan province is a treasure trove of natural beauty, cultural heritage, and delicious cuisine. It has something to offer for everyone, from history buffs to nature lovers to foodies. Yunnan is undoubtedly one of the most fascinating and unforgettable destinations in China, and it is a place that everyone should visit at least once in their lifetime.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值