AtCoder Beginner Contest 195 F. Coprime Present

链接

https://atcoder.jp/contests/abc195/tasks/abc195_f

题意

[ a , b ] [a,b] [a,b] ( b − a ≤ 72 ) (b-a\le 72) (ba72)区间内找出不同的数构成一个升序序列,序列中任意两数都互质,求这样不同的序列有多少个。

思路

gcd ⁡ ( n , m ) = gcd ⁡ ( n , n − m ) ≤ n − m \gcd(n,m)=\gcd(n,n-m)\le n-m gcd(n,m)=gcd(n,nm)nm ( n ≤ m ) (n\le m) (nm)

因为 b − a ≤ 72 b-a\le 72 ba72 所以只需要枚举 [ 2 , 72 ] [2,72] [2,72] 之间的质因数,用二进制状态存储序列中的数包含哪些质因数。

a a a 开始枚举,找出当前数中小于等于 72 72 72 的质因数,与当前数中质因数没有重集的状态可以进行转移。

代码

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long double LD;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
// head
const LL P[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
const int N=1500000;
LL dp[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    LL a,b;
    cin>>a>>b;
    dp[0]=1;
    for(LL i=a;i<=b;i++) {
        int bit=0;
        for(int j=0;j<20;j++) if(i%P[j]==0) bit|=1<<j;
        for(int j=0;j<1<<20;j++) {
            if(bit&j) continue;
            dp[bit|j]+=dp[j];
        }
    }
    LL res=0;
    for(int i=0;i<1<<20;i++) res+=dp[i];
    cout<<res<<'\n';
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值