Bzoj 1677: [usaco2005 Jan] sumsets summation

Source: Internet
Author: User
Question 1677: [usaco2005 Jan] sumsets sum time limit: 5 sec memory limit: 64 MB
Submit: 617 solved: 344
[Submit] [Status] Description

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. the cows use only numbers that are an integer power of 2. here are the possible sets of numbers that sum to 7: 1) 1 + 1 + 1 + 1 + 1 + 1 + 1 2) 1 + 1 + 1 + 1 + 1 + 2 3) 1 + 1 + 1 + 2 + 2 4) 1 + 1 + 1 + 4 5) 1 + 2 + 2 + 2 6) 1 + 2 + 4 help FJ count all possible representations for a given integer N (1 <= n <= 1,000,000 ).

A n (1 ≤ n ≤ 10 ^ 6) is given, which is obtained by adding several power numbers of 2. How many methods are there?

Input

An integer n.

Output

Number of methods. This number may be very large. Please output the last 9 digits in decimal format.

Sample input7

Sample output6

There are six methods
1) 1 + 1 + 1 + 1 + 1 + 1 + 1
2) 1 + 1 + 1 + 1 + 1 + 2
3) 1 + 1 + 1 + 2 + 2
4) 1 + 1 + 1 + 4
5) 1 + 2 + 2 + 2
6) 1 + 2 + 4
Question

This question is obviously recursive. Orz f [I] = f [I-1] + (I & 1 = 0? F [I/2]: 0)

Code
 1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 using namespace std; 4  5 const int N=1000005; 6 int f[N], n; 7   8 int main() { 9     scanf("%d",&n);10     f[1]=1;11     for(int i=2;i<=n;i++) {12         f[i]=f[i-1];13         if(!(i&1)) f[i]+=f[i>>1];14         f[i]%=1000000000;15     }16     printf("%d\n",f[n]);17     return 0;18 }
View code

 

Bzoj 1677: [usaco2005 Jan] sumsets summation

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.