BNU 4307 set problem combined mathematics

Source: Internet
Author: User

Link: http://acm.bnu.edu.cn/v3/problem_show.php? PID = 1, 4307

It turned out to be a hot-up question for the New Year's North Division competition.

It should be found from the set composed of [0, n-1], including the number of subsets of two consecutive numbers (n-1, 0 can also be.
Train of Thought: Use AA to record the question request, and use BB to record the number of subsets that meet the question condition except (n-1, 0) in [0, n-1. Calculate by recursive method.

When calculating BB [I], there are three situations: 1. subset number BB [I-1]; 2. select the I-1 but not the I-2 of the child set BB [I-2]; 3. select both the I-1 and the number of child POW I-2.

When calculating AA [I], there are four situations: 1. subset number BB [I-1]; 2. select the I-1 and select the number of child sets of the I-2 BB [I-2]; 3. both select the I-1 and select the number of sub-set BB [I-2]; (the first two cases should use the principle of rejection minus the I-2, I-1, 0 at the same time select the situation) 4. select the I-1 but do not select the I-2 and 0.

Code:

#include <iostream>#include <cstdio>using namespace std;#define MOD 8061int aa[1000005],bb[1000005],Pow[1000005];void init(){    Pow[0]=1;    for(int i=1;i<=1000000;i++)    Pow[i]=(Pow[i-1]*2)%MOD;    bb[2]=1;    bb[3]=3;    aa[2]=1;    aa[3]=4;    for(int i=4;i<=1000000;i++)    {        bb[i]=(bb[i-2]+bb[i-1]+Pow[i-2]+MOD)%MOD;        aa[i]=(bb[i-1]+Pow[i-2]*2-Pow[i-3]+bb[i-3]+MOD)%MOD;    }}int main(){    int a;    init();    while(~scanf("%d",&a))    {        printf("%d\n",aa[a]);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.