XTU 1233 Coins(DP)

题意: n个硬币摆成一排,问有连续m个正面朝上的硬币的序列种数。

很明显的DP题。定义状态dp[i][1]表示前i个硬币满足条件的序列种数。dp[i][0]表示前i个硬币不满足条件的序列种数。

那么显然有dp[i][1]=dp[i-1][1]*2+dp[i-1-m][0].

如果前i-1个硬币满足条件,那么第i个硬币无论怎么样都满足条件。如果前i-1-m个硬币不满足条件,那么只需要再添加m个正面朝上的硬币即可。

dp[i][0]=2^i-dp[i][1].

于是最后的答案就是dp[n][1].

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-7
# define MOD 1000000007
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
void Out(int a) {
    if(a<0) {putchar(‘-‘); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+‘0‘);
}
const int N=1000005;
//Code begin...

LL dp[N][2], p[N];

void init(){p[0]=1; FO(i,1,N) p[i]=p[i-1]*2%MOD;}
int main ()
{
    int T, n, m;
    scanf("%d",&T); init();
    while (T--) {
        scanf("%d%d",&n,&m); mem(dp,0);
        dp[m][1]=1; dp[m][0]=((p[m]-dp[m][1])%MOD+MOD)%MOD;
        FO(i,0,m) dp[i][0]=p[i];
        FOR(i,m+1,n) dp[i][1]=(dp[i-1][1]*2+dp[i-1-m][0])%MOD, dp[i][0]=((p[i]-dp[i][1])%MOD+MOD)%MOD;
        printf("%lld\n",dp[n][1]);
    }
    return 0;
}

时间: 2024-08-10 19:14:47

XTU 1233 Coins(DP)的相关文章

HDU 1398 Square Coins(DP)

Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8800    Accepted Submission(s): 5991 Problem Description People in Silverland use square coins. Not only they have square shapes but

hdu 2844 Coins DP

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7716    Accepted Submission(s): 3158 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

uva--562Dividing coins +dp

题意: 给定一堆硬币,然后将他们分成两部分,使得两部分的差值最小;输出这个最小的差值. 思路: 想了好久都没想到一个合适的状态转移方程.后面看了别人的题解后,才知道能够转成背包问题求解. 我们将全部的硬币和的一半作为背包容量,然后将硬币的代价看成其本身的面值.然后背包中能装的最大容量 就是当中一个人分得硬币数. 代码例如以下: #include<iostream> #include<cstdio> #include<cstring> using namespace st

[email&#160;protected] [322] Coin Change (Dynamic Programming)

https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot

322.零钱兑换(动态规划)

给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/coin-change著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 以前我会直接贪婪算法,然后解不出来,现在第一想法就是动态规划:把每一步的最优解都算出来,取最后一项返回. 本题状态转移方程: dp[ i ]

Coins(2015嘉杰信息杯+dp)

Coins Accepted : 78   Submit : 188 Time Limit : 1000 MS   Memory Limit : 65536 KB Coins Problem Description: Duoxida buys a bottle of MaiDong from a vending machine and the machine give her n coins back. She places them in a line randomly showing hea

xtu DP Training B. Collecting Bugs

B. Collecting Bugs Time Limit: 10000ms Memory Limit: 64000KB 64-bit integer IO format: %lld      Java class name: Main Special Judge Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stuff, he collects s

poj 1742 Coins(dp之多重背包+多次优化)

Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact pri

xtu DP Training C.炮兵阵地

炮兵阵地 Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 118564-bit integer IO format: %lld      Java class name: Main 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P"表