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對答案的貢獻即可。(具體公式請參看程序)

代碼:

#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 = 2e3+10;
const int mod = 1000000007;

int f[maxn];

int main(){
    f[0] = 1;
    for(int i=1; i<100; i++)
        f[i] = (f[i-1]<<1)%mod;

    int T = read();
    for(int cas=1; cas<=T; cas++){
        int n = read();
        int ans = 0;
        for(int i=1; i<=n; i++){
            ans += 1LL * (f[2*n/i-1]-1) * f[2*n-i-(2*n/i-1)]%mod;
            ans %= mod;
        }
        printf("Case %d: %d\n",cas,ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章