Wannafly2016-12-27 SPOJ-INTSUB 数学

题目链接:

http://www.spoj.com/problems/INTSUB/

题意:

给定一个集合,该集合由1,2,3….2n组成,n是一个整数。问该集合中有趣子集的数目,答案mod1e9+7。
x的子集合有趣定义为,该子集中至少有两个数,a和b,b是a的倍数且a是集合中最小的元素。

题解:

枚举子集中最小的元素,然后确定其他的元素。
假设现在最小元素为a,则有2n/a-1个大于a的元素是a的倍数,且这些元素必须在子集中出现至少一个,剩下的大于a的数取和不取对答案不造成影响。
累计不同的a对答案的贡献即可。(具体公式请参看程序)

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define MS(a) memset(a,0,sizeof(a))
 5 #define MP make_pair
 6 #define PB push_back
 7 const int INF = 0x3f3f3f3f;
 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 9 inline ll read(){
10     ll x=0,f=1;char ch=getchar();
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 //////////////////////////////////////////////////////////////////////////
16 const int maxn = 2e3+10;
17 const int mod = 1000000007;
18 
19 int f[maxn];
20 
21 int main(){
22     f[0] = 1;
23     for(int i=1; i<100; i++)
24         f[i] = (f[i-1]<<1)%mod;
25 
26     int T = read();
27     for(int cas=1; cas<=T; cas++){
28         int n = read();
29         int ans = 0;
30         for(int i=1; i<=n; i++){
31             ans += 1LL * (f[2*n/i-1]-1) * f[2*n-i-(2*n/i-1)]%mod;
32             ans %= mod;
33         }
34         printf("Case %d: %d
",cas,ans);
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/yxg123123/p/6827578.html