题目链接:
http://codeforces.com/contest/550/problem/B
题意:
有n门课,然后让你选择一些课,要求这些课程的和小于等于r,大于等于l,最大值减去最小值至少为x
然后问你有多少种分法
题解:
数据范围小 n<=15 所以可以把n门课 变成二进制的状态
也可以直接dfs
代码:
dfs ~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;
int n,vis[20];
ll le,ri,x,a[20];
int ans;
void dfs(int t, ll sum, ll mi,ll mx){
if(sum > ri) return ;
if((mx-mi)>=x && (sum>=le) && (sum<=ri))
ans++;
for(int i=t; i<=n; i++){
if(!vis[i]){
vis[i] = 1;
dfs(i,sum+a[i],min(a[i],mi),max(a[i],mx));
vis[i] = 0;
}
}
}
int main(){
n = read(),le=read(),ri=read(),x=read();
for(int i=1; i<=n; i++)
a[i] = read();
dfs(1,0,INF,-1);
cout << ans << endl;
return 0;
}
每位表示状态 选和不选
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;
ll n,le,ri,x,a[20];
int ans;
int main(){
n = read(),le=read(),ri=read(),x=read();
for(int i=0; i<n; i++)
a[i] = read();
for(int i=0; i<(1<<n); i++){
ll cnt=0,sum=0,mi=INFLL,mx=-1;
for(int j=0; j<n; j++){
if(i&(1<<j)){
cnt++;
sum += a[j];
mi = min(mi,a[j]);
mx = max(mx,a[j]);
}
}
if(cnt>=2 && (mx-mi)>=x && sum>=le && sum<=ri)
ans++;
}
cout << ans << endl;
return 0;
}