Ada and Coins

Ada and Coins

题意:钱包里有n种钱,然后有m次询问,询问[l,r]区间内能被表示的个数有几个。

题解:这道题是群主推荐我写的,然后让我用bitset去写,他说 操作32个bitset需要的时间 == 操作一个int所需要的时间, 所以有些题目用bitset去处理就能快了。

      这道题目的思路就是瞎几把乱写, 然后求一个前缀和就好了。

  

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 1e5+10;
17 bitset<N> b;
18 int tot[N];
19 int main(){
20     //Fopen;
21     b[0] = 1;
22     int n, m;
23     scanf("%d%d", &n, &m);
24     int t;
25     for(int i = 1; i <= n; i++){
26         scanf("%d", &t);
27         b |= (b << t);
28     }
29     tot[0] = 1;
30     for(int i = 1; i < N; i++)
31         tot[i] = tot[i-1] + b[i];
32     int l, r;
33     while(m--){
34         scanf("%d%d",&l,&r);
35         printf("%d\n",tot[r] - tot[l-1]);
36     }
37     return 0;
38 }
View Code

猜你喜欢

转载自www.cnblogs.com/MingSD/p/8931389.html