UVA 10081 Tight numbers(POJ 2537)

  直接看代码就OK。思路比较简单。就是注意概率要在转移过程中算出来。不能算成成立的方案书除以总方案数(POJ的这道题可以这么干。数据很水么。另外POJ要用%.5f,%.5lf 会WA。)

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
double dp[105][15];
int N,K;
void slove()
{
    if (K <= 1) {puts("100.00000");return;}
    for (int i = 0 ;i < 105; i++) for (int j = 0;j <15; j++) dp[i][j]=0.0;
    for (int i = 0 ;i <= K; i++) dp[1][i]=100.0/(double)(K+1);
    for (int i = 2; i <= N; i++)
    {
        dp[i][0] = 1.0/(double)(K+1) * ( dp[i-1][0] + dp[i-1][1]);
        for (int j = 1; j <= K ; j++)
        {
            if (j == K) dp[i][j] = 1.0/(double)(K+1) * (dp[i-1][K] + dp[i-1][K-1]);
            else  dp[i][j] = 1.0/(double)(K+1) * (dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1]);
        }
    }
    double ans=0.0;
    for (int i = 0 ;i <= K; i ++) ans+=dp[N][i];
    printf("%.5lf\n",ans);
}
int main()
{
    while (scanf("%d%d",&K,&N)!=EOF)
    slove();
    return 0;
}
时间: 2024-08-28 18:51:10

UVA 10081 Tight numbers(POJ 2537)的相关文章

Uva 10081 Tight words (概率DP)

Time limit: 3.000 seconds Given is an alphabet {0, 1, ... , k}, 0 <= k <= 9 . We say that a word of length n over this alphabet is tightif any two neighbour digits in the word do not differ by more than 1. Input is a sequence of lines, each line con

UVa 10081 - Tight Words

題目:有一個集合{0,1,..,k},問其中元素組成的長為n的串中,相鄰元素差值不超過2的串的數比例. 分析:動態規劃.概率dp. 初始狀態:f(i,1)= 1 /(k+1): 狀態轉移:f(i,j)= sum(f(i-1,t))/ (k+1) { 其中,t為和j相差不超過2的元素 }. 說明:好久沒刷題了╮(╯▽╰)╭. #include <algorithm> #include <iostream> #include <cstdlib> #include <c

poj 2537 Tight words 概率dp

分析: 用计数dp思想:DP[I][J]=(DP[I-1][J-1]+DP[I-1][J]+DP[I-1][J+1]),最后再除pow(k+1,n)容易爆精度,改用概率dp思想DP[I][J]=(DP[I-1][J-1]+DP[I-1][J]+DP[I-1][J+1])/(k+1)即可. 代码: //poj 2537 //sep9 #include<iostream> using namespace std; double dp[128][16]; int main() { int k,n;

UVA - 12046 Great Numbers

Description Problem G - Great Numbers In this problem you have to count the number of great numbers of length n. Here a great number must have the following property: the number must be divisible by all of its decimal digits. it does not contain any

UVA - 471 Magic Numbers

Description  Magic Numbers  Write a program that finds and displays all pairs ofintegers and such that: neither nor have any digits repeated; and , where N is a given integer; Input and Output The input file consist a integer at the beginning indicat

Uva - 12050 Palindrome Numbers【数论】

题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出AC代码: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int maxn=3010; 5

UVa 127 - &quot;Accordian&quot; Patience POJ 1214 链表题解

UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比较简单,这里纯粹使用指针做了,非常麻烦的指针操作,一不小心就错.调试起来还是非常费力的 本题理解起来也是挺费力的,要搞清楚如何模拟也不容易啊,读题要很仔细. 纯指针的操作挺快的吧.不过POJ 0ms,而UVa就0.2左右了. 三相链表: 1 只要有叠起来的牌,那么就使用一个down指针指向下面的牌就可以了. 2 使用双向链表,可以方便前后遍历. 3 记得有了更新牌之后,又要重新开始检查是否需要更新牌,这是

UVA 136 Ugly Numbers

原题代号:UVA 136 原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=72 题目原题: Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5,

Sum of Consecutive Prime Numbers POJ - 2739

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The intege