BZOJ1677: [Usaco2005 Jan]Sumsets 求和
1677: [Usaco2005 Jan]Sumsets 求和
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 570 Solved: 310
[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).
给出一个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
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 1000000+1000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define mod 1000000000
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n;
ll f[maxn];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
f[]=;
for(int i=;i<=;i++)
{
int x=<<i;
if(x>n)break;
for(int j=x;j<=n;j++)f[j]+=f[j-x],f[j]%=mod;
}
printf("%lld\n",f[n]);
return ;
}
UPD:
原来有递推式啊。。。
注意到如果i是奇数的话,一定会有一个1存在,所以f[i] = f[i-1] % mod;当i是偶数时,可以看成俩种情况,一种是2个1加上一个f[i-2],一种情况是所有都是2的倍数,可以都除以2再搞,然后就可以得到递推式f[i] = (f[i/2] + f[i-2]) % mod; 综合起来就是此题的递推关系式
代码:
BZOJ1677: [Usaco2005 Jan]Sumsets 求和的更多相关文章
- 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 ...
- 1677: [Usaco2005 Jan]Sumsets 求和
1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 626 Solved: 348[Submi ...
- 【BZOJ1677】[Usaco2005 Jan]Sumsets 求和 递推
... #include <iostream> using namespace std; ]; int n,i; int main() { cin>>n; f[]=; ;i&l ...
- 【BZOJ】1677: [Usaco2005 Jan]Sumsets 求和(dp/规律)
http://www.lydsy.com/JudgeOnline/problem.php?id=1677 完全背包很容易想到,将1,2,4...等作为物品容量即可. 然后这题还有一个递推式 f[i]= ...
- BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...
- [Usaco2005 Jan]Sumsets 求和
Description Farmer John commanded his cows to search for different sets of numbers that sum to a giv ...
- 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位所有因子乘 ...
- BZOJ1679: [Usaco2005 Jan]Moo Volume 牛的呼声
1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 723 Solved: 346[ ...
随机推荐
- JAVA大数类
JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...
- cocos2d-x3.0 Physics新的物理引擎
1.说明: 3.0以后将box2d和chipmunk这两个物理引擎进行了封装,使用起来很的便利 2.详细用法: 1.创建物理世界场景 auto scene = Scene::createWithPhy ...
- HeadFirst设计模式笔记:(六)命令模式 —— 封装调用
1.概念 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化.分离变化与不变的因素. 在面向对象的程序 ...
- 三种root的修补方式
三种root的修补方式 system/core/adb/abd.c adbd漏洞,请看abd.c的第917行/* then switch user and group to "shell&q ...
- Spring 3整合Quartz 2实现定时任务--转
常规整合 http://www.meiriyouke.net/?p=82 最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之 ...
- 案例:计算1!+2!+3!+......+n!
/* * 1!+2!+3!+......+n! * */ import java.util.Scanner; public class ForTest{ public static void main ...
- 转载:浅析C#深拷贝与浅拷贝
原文地址 :http://www.cnblogs.com/xugang/archive/2010/09/09/1822555.html 感谢博主分享! 也许会有人这样解释C# 中浅拷贝与深拷贝区别 ...
- Asp.net Core 部署到Azure.cn的一个小问题
前一段尝试在azure.cn上部署Aps.net Core未成功,报503错误!在网上查到是Azure.cn的问题,未能完美支持Asp.net Core! Asp.net Core发表正式版了,又尝试 ...
- ASP.NET 实现上一篇文章 下一篇文章
select top 1 * from job_hrnews where newsid>162 --下一篇 select top 1 * from job_hrnews where newsi ...
- Ci 简单分页,保证能实现
某晚,自己写项目的时候去找资料,关于CI分页的, 发现百度出来的前几名的基本都是写的都是垃圾, 要么是实现不了,要么就是坑逼 所以我自己在这里写一个,不是很完美,只是说是简单的实现了原理 有了最基本的 ...