POJ 2229(Sumsets)

题目链接:http://poj.org/problem?id=2229

思路:“动态规划”问题

   只需要列三个连续数字N即可发现:1.N为奇数时,f(n)=f(n-1)

                     2.N为偶数时,f(n)=f(n-1)+f(n/2)

                     因为此时N-1为基数,N-1情况的每一行第一个数一定是1,所以加上一个1时,就相当于在前面直接加一个1或者与 “后面” 的1组成2。

ac代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string.h>
#define MOD 1000000000
int N;
int dp[1000005];

int main(void){
    int num=1;
    scanf("%d",&N);
    dp[1]=1;
    for(int i=2;i<=N;i++){
        if(i&1==1){
            dp[i]=dp[i-1];
        }else{
            dp[i]=(dp[i-1]+dp[i>>1])%MOD;
            // 此处一定要取模
        }
    }
    printf("%d\n",dp[N]);

    return 0;
}

有一个小技巧:

按位操作

原文地址:https://www.cnblogs.com/jaszzz/p/12590888.html

时间: 2024-08-29 21:23:57

POJ 2229(Sumsets)的相关文章

POJ 2253-Frogger (Prim)

题目链接:Frogger 题意:两只青蛙,A和B,A想到B哪里去,但是A得弹跳有限制,所以不能直接到B,但是有其他的石头作为过渡点,可以通过他们到达B,问A到B的所有路径中,它弹跳最大的跨度的最小值 PS:最小生成树过的,刚开始用Dijstra做,Kao,精度损失的厉害,对于Dijksra的变形不大会变啊,看了Discuss有人用最小生成树过,我一划拉,还真是,敲了,就过了,等会研究研究最短路的各种变形,有模板不会变,不会灵活应用,渣渣就是渣渣. ME               Time 10

poj 1861(最小生成树)

Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access

POJ 2352 (stars)

[题意描述] 就是给定n个星星的x,y坐标,y坐标按照从小到大的顺序进行排列,x坐标随机排列.下面求对于每个星星而言,其它星星的x,y的坐标都小于等于该星星的数目,然后输出所有的情况. [思路分析] 我们这道题可以采用树状数组求解,将x+1作为树状数组的底标. [AC代码] #include<iostream> #include<bitset> #include<cstdio> #include<cstring> #include<algorithm&

poj Sudoku(数独) DFS

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13665   Accepted: 6767   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

POJ 3419 (rmq)

这道题是rmq,再加上一个解决溢出. 刚开始我也想过用rmq,虽然不知道它叫什么,但是我知道应该这样做.可是后来没想到这道题的特殊性,也就是解决溢出的方法,就放弃了. rmq可以用线段树,也可以用dp.  这道题都可以过的,而且线段树要快一些. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define m

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (

poj数论(转)

1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式.    *简单题:(直接用套公式就可以了)    pku2409 Let it Bead       pku2154 Color    pku1286 Necklace of Beads    *强烈推荐:(这题很不错哦,很巧妙)    pku2888 Magic Bracelet2.置换,置换的运算 置换的概念

poj 1200 (hash)

Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted: 6513 Description Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a gi

Booksort POJ - 3460 (IDA*)

Description The Leiden University Library has millions of books. When a student wants to borrow a certain book, he usually submits an online loan form. If the book is available, then the next day the student can go and get it at the loan counter. Thi