前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 2015 Multi-Universi

原 2015 Multi-Universi

作者头像
不高不富不帅的陈政_
发布2018-05-18 15:40:40
5140
发布2018-05-18 15:40:40
举报
文章被收录于专栏:聊聊技术聊聊技术

1011.Key Set

Summary

For a given set {1, 2, . . . , nn}, you are to find the number of nonempty subsets with even sum.

Solution

Let aa be the number of even integers and bb be the number of even integers. The answer is:

2^{a}(\begin{pmatrix}b\0 \end{pmatrix}+\begin{pmatrix}b\2 \end{pmatrix}+2a((b0)+(b2)+…)=2^{a+b-1}=2^{n-1}-1)=2a+b−1=2n−1−1

Time complexity: O(log\ n)O(log n)

----------------------大家好我是分割线------------------------

    作为一支酱油队,我们不负众望的只做出了水题(1011)。下面附上代码:

#include <stdio.h>
#include <iostream>
#define NUM 1000000007
using namespace std;

long long d[10000000];

long long def(int n)
{
    if(n<10000000)
    {
        return d[n];
    }
    return (def(n/2)*def(n-n/2))%NUM;
}

int main()
{
    long long n;
    int t;

    d[0]=1;
    for(int i=1;i<10000000;i++)
    {
        d[i]=(d[i-1]*2)%NUM;
    }

    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        long long ans = def(n-1)-1;
        printf("%lld\n",ans);
    }
    return 0;
}

    我们做着做着,突然发现如何快速得到2的n次方似乎是个问题,又不能开出10^9大小的数组,所以干脆开了个10^8大小的数组保存2^i,当所给的n大于10^8时,二分即可。递归树只有5层,速度还是很快的。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1011.Key Set
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档