试题D:抽刀断水水更流,举杯销愁愁更愁 10’

写在前面: 原本应该dfs来做,但是吃饭时想到一个奇妙的方法,就写个博客记录一下

题目: 传送门
思路: 用0000000000000000000000(22个0)到1111111111111111111111(22个1)表示22个数字的取舍,一个for循环加判断就vans了
Code:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;

int arr[]={3,5,7,11,13,19,23,29,31,37,41,53,59,61,67,71,97,101,127,197,211,431};
bool can[2000];
vector<int> v;
bool less_than_12(int x) {
    int cnt = 0;
    while (x > 0) {
        if (x & 1 == 1)
            cnt++;
        x >>= 1;
    }
    return cnt <= 12;
}
void mark(int x) {
    v.clear();
    while (x > 0) {
        v.push_back(x & 1);
        x >>= 1;
    }
    if (v.size() != 22) {//不满足22个,后面补0
        while (v.size() != 22)
            v.push_back(0);
    }
    int sum = 0;
    for (int i=v.size()-1;i>=0;i--) {
        if (v[i] == 1) {
            sum += arr[v.size() - i - 1];
        }
    }
    can[sum] = true;
}
int main()
{
    for (int i=0;i<=4194303;i++){//从0000000000000000000001到1111111111111111111111
        if (less_than_12(i)) {
            mark(i);
        }
    }
    
    int cnt = 0;
    for (int i=0;i<=1694;i++) {
        if (!can[i])
            cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值