hihocoder 1754
题目链接: https://hihocoder.com/problemset/solution/1327177
解题思路:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int MAXN = 100005; 6 const LL MOD7 = 1e9+7; 7 8 LL C; 9 int n; 10 int a[64]; 11 int cnt[64]; 12 13 map<LL,int> ind2; 14 map<int,LL> pow2; 15 16 void init() 17 { 18 pow2[0] = 1; 19 ind2[1] = 0; 20 for (int i=1;i<63;++i) 21 { 22 pow2[i] = pow2[i-1]*2; 23 ind2[pow2[i]]=i; 24 } 25 } 26 27 void work() 28 { 29 cnt[0]=a[0]; 30 for (int i=1;i<63;++i) 31 { 32 cnt[i]=cnt[i-1]+a[i]; 33 } 34 LL tmp=C; 35 LL ans = 0LL; 36 for (int i=62;i>=0;--i) 37 { 38 if (tmp>=pow2[i]) 39 { 40 if (a[i]==1) { 41 if (cnt[i]>=1) ans += pow2[cnt[i]-1]; 42 } else { 43 ans += pow2[cnt[i]] - 1; 44 break; 45 } 46 tmp-=pow2[i]; 47 } 48 } 49 printf("%lld\n",ans); 50 } 51 52 int main() 53 { 54 init(); 55 #ifndef ONLINE_JUDGE 56 freopen("test.txt","r",stdin); 57 #endif // ONLINE_JUDGE 58 while ( scanf("%d%lld",&n,&C)!=-1 ) { 59 LL d; 60 for (int i=1;i<=n;++i) 61 { 62 scanf("%lld",&d); 63 a[ind2[d]]=1; 64 } 65 work(); 66 } 67 return 0; 68 }