zoukankan      html  css  js  c++  java
  • 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

    7

    Sample Output

    6

    有以下六种方式
    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

    题解:
    又一道无限背包。。。一中午3道。。。
    代码:
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1000000000
    13 #define maxn 1000000+1000
    14 #define maxm 500+100
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 #define mod 1000000000
    19 using namespace std;
    20 inline int read()
    21 {
    22     int x=0,f=1;char ch=getchar();
    23     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    24     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    25     return x*f;
    26 }
    27 int n;
    28 ll f[maxn];
    29 int main()
    30 {
    31     freopen("input.txt","r",stdin);
    32     freopen("output.txt","w",stdout);
    33     n=read();
    34     f[0]=1;
    35     for(int i=0;i<=30;i++)
    36     {
    37         int x=1<<i;
    38         if(x>n)break;
    39         for(int j=x;j<=n;j++)f[j]+=f[j-x],f[j]%=mod;
    40     }
    41     printf("%lld
    ",f[n]);
    42     return 0;
    43 }
    View Code

    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; 综合起来就是此题的递推关系式

    代码:

  • 相关阅读:
    HDU 1711 Number Sequence
    PAT L2-016 愿天下有情人都是失散多年的兄妹
    PAT L2-024 部落
    斜率优化DP小结(含凸优化)
    jzoj 4475. 【GDOI2016模拟4.25】征途
    jzoj 6271. 2019.8.4【NOIP提高组A】锻造 (forging)
    jzoj 6273. 2019.8.4【NOIP提高组A】欠钱 (money)
    jzoj 6272. 2019.8.4【NOIP提高组A】整除 (division)
    2019.08.04【NOIP提高组】模拟 A 组 总结
    jzoj 2184. 羊羊列队
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3941478.html
Copyright © 2011-2022 走看看