牛客-牛牛的零食

链接:https://ac.nowcoder.com/acm/contest/6164/B
来源:牛客网
 

题目描述

牛牛是怎么胖的呢?当然是因为他太热爱吃零食了,牛牛给他的每一份零食编了号,每次他会拿出编号在[a,b]区间里能被8整除却不能被另外一些数中的任意一个整除的零食吃掉。现在请你帮他算一算他这一次到底能吃多少份零食吧?

 

 

 

输入描述:

 

第一行一个数n,代表不能被整除的数的个数。

第二行n个数,中间用空格隔开。

第三行两个数a,b,中间一个空格。

输出描述:

一个整数,为牛牛能吃到的零食份数,也就是[a,b]间中能被8整除却不能被给出的那n个数中任意一个整除的数的个数。

示例1

输入

 

3
7764 6082 462
2166 53442

输出

 

6378

说明

1≤n≤15,1≤a≤b≤10^9

思路:通过深搜以及对最小公倍数和约数的理解来解决,具体见代码。

记录一下这样的解题思路。。。 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#define cla(a, sum) memset(a, sum, sizeof(a))
#define rap(i, m, n) for(int i=m; i<=n; i++)
#define rep(i, m, n) for(int i=m; i>=n; i--)
#define bug printf("???\n")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const int Inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 3e4;
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

ll n,a,b;
ll c[20],ans;

ll gcd(ll x,ll y){
	return y==0?x:gcd(y,x%y);
}
ll lcm(ll x,ll y){
	return x*y/gcd(x,y);
}

void dfs(ll cnt,ll tmp,ll flag){
	if(cnt>n){
		if(flag)ans+=b/tmp-(a-1)/tmp;
		else ans=ans-(b/tmp-(a-1)/tmp);
		return ;
	}
	dfs(cnt+1,tmp,flag);
	dfs(cnt+1,lcm(tmp,c[cnt]),flag^1);
}

int main()
{
    cin>>n;
    rap(i,1,n)scanf("%lld",&c[i]);
    scanf("%lld%lld",&a,&b);
    ans=0;
    dfs(1,8,1);
    cout<<ans;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值