zoukankan      html  css  js  c++  java
  • [USACO09MAR]Cow Frisbee Team

     嘟嘟嘟

    这个是一个很明显的dp,遇到这种倍数的问题的,就令dp[i][j]表示选到了第 i 只牛(不是选了 i 只牛),sum(Ri) % f == j 的方案数,则,

        dp[i][j] = dp[i - 1][j] + dp[i - 1][(j + f - a[i] % f) % f]

    等式右边第一项表示第 i 只牛不选,第二项表示第 i 只牛选了,j + f 是为了防止出现负数。

    初始化令dp[0][0] = 1,但实际上这个状态应该是0,所以随后答案是dp[n][0] - 1.

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #include<cctype>
     8 #include<vector>
     9 #include<stack>
    10 #include<queue>
    11 using namespace std;
    12 #define enter puts("") 
    13 #define space putchar(' ')
    14 #define Mem(a) memset(a, 0, sizeof(a))
    15 typedef long long ll;
    16 typedef double db;
    17 const int INF = 0x3f3f3f3f;
    18 const db eps = 1e-8;
    19 const int mod = 1e8;
    20 const int maxn = 1e3 + 5;
    21 inline ll read()
    22 {
    23     ll ans = 0;
    24     char ch = getchar(), last = ' ';
    25     while(!isdigit(ch)) {last = ch; ch = getchar();}
    26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
    27     if(last == '-') ans = -ans;
    28     return ans;
    29 }
    30 inline void write(ll x)
    31 {
    32     if(x < 0) x = -x, putchar('-');
    33     if(x >= 10) write(x / 10);
    34     putchar(x % 10 + '0');
    35 }
    36 
    37 int n, f, a[maxn << 1];
    38 int dp[maxn << 1][maxn];
    39 
    40 int main()
    41 {
    42     n = read(); f = read();
    43     for(int i = 1; i <= n; ++i) a[i] = read();
    44     dp[0][0] = 1;
    45     for(int i = 1; i <= n; ++i)
    46         for(int j = f - 1; j >= 0; --j)
    47         {
    48             dp[i][j] = dp[i - 1][j] + dp[i - 1][(j + f - a[i] % f) % f];
    49             dp[i][j] %= mod;
    50         }
    51     write(dp[n][0] - 1); enter;
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    java中&和&&是怎么运算的
    struts中ActionForward 使用mapping.findForward如何传递get参数
    EL表达式_详解
    JSTL标签_详解
    inner join, left join, right join, full join 的区别
    CentOS7部署FastDFS+nginx模块
    一个实例明白AutoResetEvent和 ManulResetEvent的用法
    C#防止在画面上闪烁的Button
    C#中给Label控件设置BackgroundImage属性
    浅析C#异步操作
  • 原文地址:https://www.cnblogs.com/mrclr/p/9569153.html
Copyright © 2011-2022 走看看