uva 1350 - Pinary(dp+计数)

题目链接:uva
1350 - Pinary

题目大意:给出n,输出第n给Pinary Number,Pinary Number为二进制数,并且没有连续两个1相连。

解题思路:dp[i]表示到第i位有dp[i]种,于是给定n,一层循环判断dp[i]≤n的话,就输出1,并且n减掉dp[i],注意输出0的时候,不能输出前导0.

#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 50;
ll dp[N];

void init () {
    memset(dp, 0, sizeof(dp));
    dp[0] = dp[1] = 1;
    for (int i = 2; i <= 40; i++)
        dp[i] = dp[i-1] + dp[i-2];
}

void solve (ll n) {
    bool flag = false;    

    for (int i = 40; i; i--) {
        if (n >= dp[i]) {
            printf("1");
            n -= dp[i];
            flag = true;
        } else if (flag)
            printf("0");
    }
    printf("\n");
}

int main () {
    init ();
    int cas;
    ll n;
    scanf("%d", &cas);
    for (int i = 0; i < cas; i++) {
        scanf("%lld", &n);
        solve(n);
    }
    return 0;
}

uva 1350 - Pinary(dp+计数)

时间: 2024-10-09 13:35:14

uva 1350 - Pinary(dp+计数)的相关文章

UVA 1350 - Pinary(数论+递推)

题目链接:1350 - Pinary 题意:二进制数,不能有连续的1,给定第n个数字,输出相应的二进制数 思路:先是递推,求出由n位组成的数字中有几个满足条件 dp[i] = dp[i - 1] + dp[i - 2],考虑最后一位放0和倒1位放0的情况. 然后用一个sum[i]记录满足<=i位一共的情况 接着利用二分找到给定的n > sum[i - 1],i的最大值,这个就是所求的答案的最高位. 因为如果这位放1,那么就会一共多sum[i - 1] + 1个数,那么就还需要添加n - (su

UVA - 1350 Pinary (递推)

Description ``Pinary" number is a positive number using only two digits ``0" and ``1" with usual rule that it must not begin with a 0, and the additional rule that two successive digits must not be both ``1". This means that the factor

[uva 1350]数位dp+二分

题目链接:https://vjudge.net/problem/38405 #include<bits/stdc++.h> using namespace std; long long dp[64][2]; int b[64]; long long dfs(int pos,int preok,int pre1) { if (pos==-1) return 1; if (preok && dp[pos][pre1]!=-1) return dp[pos][pre1]; int u

UVA 1393 - Highways (容斥原理计数)

题目链接:1393 - Highways 题意:给定一个n * m的点阵,问两两相连后,能组成多少条至少穿过两个点的直线,并且不是水平或垂直的 思路:找过两点的线段,由于是整数坐标,只要他的斜率不是整数,即x / y不是整数就能满足答案,然后先记录下这所有的位置,然后利用容斥原理求出对应每个点可以连出多少条这样的线段,最后去求和,求和的时候要注意,由于有一些是重复计算了,比如1 1 和 2 2 连,2 2 和 3 3 连,这样其实是算一条的,所以最后在求和的时候要扣掉重复的部分,直接减去sum[

uva 12723 概率dp

Dudu is a very starving possum. He currently stands in the first shelf of a fridge. This fridge iscomposed of N shelves, and each shelf has a number Qi (1 ≤ i ≤ N) of food. The top shelf, whereDudu is, is identified by the number 1, and the lowest is

uva 1510 - Neon Sign(计数)

题目链接:uva 1510 - Neon Sign 题目大意:给定n个点,任意三点不共线,并且两两点之间有一条线,给定线的颜色.问说有多少个三角形三边同色. 解题思路:对于每个点,记录该点黑色边的数量和红色边的数量,考虑以该点为顶点的三角形,从红色边中选一条,黑色边中选一条,组成的三角形一定是不满足的.因为一个不同色三角形会有两个点满则,所以考虑了两次.用总的个数减掉不同色的即可. #include <cstdio> #include <cstring> #include <

ZOJ3380- Patchouli&#39;s Spell Cards(概率DP+计数)

Patchouli's Spell Cards Time Limit: 7 Seconds      Memory Limit: 65536 KB Patchouli Knowledge, the unmoving great library, is a magician who has settled down in the Scarlet Devil Mansion (紅魔館). Her specialty is elemental magic employing the seven ele

uva 294 - Divisors(枚举+计数)

题目连接:uva 294 - Divisors 题目大意:给出一个范围L~U,问说在该范围中因子数最多的数是多少. 解题思路:枚举L~U中的数,将数分解成质因子,利用乘法原理求总因子数. #include <cstdio> #include <cstring> #include <cmath> int countFactor (int x) { int ans = 1; int m = sqrt(x+0.5); for (int i = 2; i <= m; i+

uva 11133 - Eigensequence(dp)

题目链接:uva 11133 - Eigensequence 题目大意:给定一个序列a,能够确定一个序列b,要求: 1)b[1]=a[1] 2)a[j?1]<b[j]≤a[j]且b[j]a[j]?a[j?1]为整数,j>1的时候 假设对于每一个ai=bi,则称b序列为Eigensequence序列. 如今给定a1和an,问有多少个Eigensequence序列. 解题思路:dp[i][j]表示第i个数为j的情况有多少种,假设k整除k-j,dp[i+1][k]+=dp[i][j]. #inclu