首页 技术 正文
技术 2022年11月17日
0 收藏 714 点赞 2,512 浏览 1110 个字

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).

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input

7

Sample Output

6理解两个点:第一个就是滚动数组;
第二个就是dp[]含义;滚动数组,思维还是二维的,表现形式是一维。注意滚动数组内层循环要从大的开始,因为这是一种迭代,外层循环每循环一次d[j]就会改变,之前的就会被覆盖。
至于为什么要从大的开始呢?因为从小的开始d[j] j 0...C,之后有dp[j-w[i]]就不会使用上层循环,而是会用这层循环前面更新的值,这样就错了。//提示:这个用于01背包!!!!此题是完全背包用不到dp[j]这个含义是变成j,需要多少步。自己可以跟进一下。
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[25];
const int mo=1000000000;
int dp[1100000];
void pow1()
{
a[0]=1;
for(int i=1;i<21;i++)
{
a[i]=a[i-1]*2;
}
return;
}int main()
{
int n;
cin>>n;
pow1();
dp[0]=1;
for(int i=0;i<21;i++)
{
if(a[i]>n) break;
for(int j=a[i];j<=n;j++)
{
dp[j]=(dp[j]+dp[j-a[i]])%mo;
}
}
printf("%d\n",dp[n]);
return 0;
}

  


    					
上一篇: PyQt5--QSplitter
下一篇: Ajax进阶
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898