链接: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; }