【BZOJ】1677: [Usaco2005 Jan]Sumsets 求和(dp/规律)
http://www.lydsy.com/JudgeOnline/problem.php?id=1677
完全背包很容易想到,将1,2,4...等作为物品容量即可。
然后这题还有一个递推式
f[i]==f[i-1], 当i%2==1
f[i]==f[i-1]+f[i/2], 当i%2==0
当i为奇数时,我们可以看为i-1加上一个1的情况,那么只有f[i-1]中情况(因为每种情况只是多了一个1)
当i为偶数时,分为2种情况,含有1和不含有1,当含有1时,那么情况就是f[i-1],当不含有1时,情况就是f[i/2],因为不含有1他们都可以被2整除
背包:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000005, MD=1000000000;
int f[N], n; int main() {
read(n);
f[0]=1;
for1(i, 0, 30) {
int s=1<<i;
if(s>n) break;
for(int j=s; j<=n; ++j)
{ f[j]+=f[j-s]; f[j]%=MD; }
}
print(f[n]);
return 0;
}
递推:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000005;
int f[N], n; int main() {
read(n);
f[1]=1;
for1(i, 2, n) {
f[i]=f[i-1];
if(!(i&1)) f[i]+=f[i>>1];
f[i]%=1000000000;
}
print(f[n]);
return 0;
}
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).
给出一个N(1≤N≤10^6),使用一些2的若干次幂的数相加来求之.问有多少种方法
Input
一个整数N.
Output
方法数.这个数可能很大,请输出其在十进制下的最后9位.
Sample Input
Sample Output
有以下六种方式
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
HINT
Source
【BZOJ】1677: [Usaco2005 Jan]Sumsets 求和(dp/规律)的更多相关文章
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )
完全背包.. --------------------------------------------------------------------------------------- #incl ...
- BZOJ 1677: [Usaco2005 Jan]Sumsets 求和
题目 1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 617 Solved: 344[Su ...
- BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...
- bzoj 1677: [Usaco2005 Jan]Sumsets 求和【dp】
设f[i]为i的方案数,f[1]=1,考虑转移,如果是奇数,那么就是f[i]=f[i-1]因为这1一定要加:否则f[i]=f[i-1]+f[i>>1],就是上一位+1或者i/2位所有因子乘 ...
- 1677: [Usaco2005 Jan]Sumsets 求和
1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 626 Solved: 348[Submi ...
- BZOJ1677: [Usaco2005 Jan]Sumsets 求和
1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 570 Solved: 310[Submi ...
- 【BZOJ1677】[Usaco2005 Jan]Sumsets 求和 递推
... #include <iostream> using namespace std; ]; int n,i; int main() { cin>>n; f[]=; ;i&l ...
- [Usaco2005 Jan]Sumsets 求和
Description Farmer John commanded his cows to search for different sets of numbers that sum to a giv ...
- BZOJ 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )
一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...
随机推荐
- 桥(Bridge)模式
Bridge定义:将抽象和行为划分开来,各自独立,但能动态的结合. 为什么使用桥模式 通常,当一个抽象类或接口有多个具体实现(concrete subclass),这些concrete之间关系可能有以 ...
- ES6 import 循环加载
1.示例 (1)a.js import {bar} from './b'; console.log('a.mjs'); console.log(bar); export let foo = 'foo' ...
- C# XMLOperate
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- BZOJ 1603 [Usaco2008 Oct]打谷机 dfs
题意:id=1603">链接 方法:暴力 解析: 搜1到n路径上的边权异或和-. 这几个水题刷的我有点-.. 代码: #include <cstdio> #include ...
- Python translate()方法
描述 Python translate() 方法根据 maketrans() 方法给出的字符映射转换表转换字符串中的字符. 语法 translate() 方法语法: Python3中: S.trans ...
- 缓存server设计与实现(五)
上次讲到lru与缓存重建,这次主要讲一下关于过期处理的一些主要问题. 在讨论这个问题之前,有个相关的问题须要大家有所了解. 就是对于一个缓存如期仅仅来说,什么东西应该缓存,什么不应该缓存.这是一个比較 ...
- 如何在Windows下面运行hadoop的MapReduce程序
在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...
- 使用ReaderWriterLock类实现多用户读/单用户写同步
使用ReaderWriterLock类实现多用户读/单用户写同步[1] 2015-03-12 应用程序在访问资源时是进行读操作,写操作相对较少.为解决这一问题,C#提供了System.Threadin ...
- win32之hPrevInstance
看了一篇博客上面说,WinMain函数中的hPrevInstance是上一个示例的句柄,我就想到可不可以使用这个来实现只能运行一个实例呢? int WINAPI WinMain(HINSTANCE h ...
- 利用eclipse的search功能搜索当前项目的源文件
当你项目的源文件太多,文件组织结构太复杂的的时候,有时候希望google来帮你一把?给个关键字就把相关的搜索结果给出来? eclipse的search功能基本上就可以完成这个任务,文件搜索,甚至JAV ...