zoukankan      html  css  js  c++  java
  • hdu Sumsets

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

    InputA single line with a single integer, N.OutputThe 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
    题目大意就是将一个整数拆分为只有 1 或者 有 1 和偶数(2的次方数)组分的几个数的和

    当于在f[n-1]的拆分结果上增加一个 1 即可;

                   当整数n为偶数时,分为两种情况,含有1 ,和不含有1;

                               含有1时,就是f[n-1],不含1时,就是f[n/2](拆分结果都除以2)的结果              

                             综上结果,得出打表公式:

                                        f[1]=1;   f[2]=2;

                                        f[n]=f[n-1]              //n是奇数

                                        f[n]=f[n-1]+f[n/2]   //n是偶数

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[1000010];
    int main()
    {
        int n;
        a[1] = 1;
        a[2] = a[3] = 2;
        a[4] = a[5] = 4;
        for(int i=6;i<=1000000;i++)
        {
            if(i%2==0)
                a[i] = (a[i-1] + a[i/2])%1000000000;//这里取i/2的值
            else a[i] = a[i-1]%1000000000;
        }
        while(cin >> n)
        {
            cout << a[n] << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    vue的学习总结---事件处理
    记一次写入导出优化,分别基于poi的SXSSF和阿里的EasyExcel
    linux系统tomcat部署SpringBoot+vue前后端分离项目正式部署
    java中各jar的作用
    记一次SpringBoot集成WebService使用Postman进行的测试
    搭建VUE
    查看window进程,并杀死进程
    卸载 nginx 彻底删除
    ubuntu16.04 安装 nginx 服务器
    Linux ubuntu ... root 初始化密码设置
  • 原文地址:https://www.cnblogs.com/l609929321/p/6946737.html
Copyright © 2011-2022 走看看