首页 > 其他分享 >【atcoder 293 E - Sugoroku 4】【动态规划,递推】

【atcoder 293 E - Sugoroku 4】【动态规划,递推】

时间:2022-10-30 23:44:05浏览次数:42  
标签:atcoder long Sugoroku newx static ans nextdp 293 MOD

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int n, m, k;
    static int MOD = 998244353;
    static long m1 = 1l; //1/m (mod MOD的值

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);

        n = in.nextInt();
        m = in.nextInt();
        k = in.nextInt();

        m1 = ksm(m, MOD - 2);

        long[] dp = new long[n + 1];
        dp[0] = 1;


        for (int i = 1; i <= k; i++) {
            long[] nextdp = new long[n + 1];
       //     nextdp =  Arrays.copyOf(dp, dp.length);
            nextdp[n] = dp[n];
            for (int x = 0; x < n; x++) {
                for (int di = 1; di <= m; di++) {
                    int newx = x+di;
                    if (x + di >= n) {
                        newx = n - (newx - n);
                    }
                    nextdp[newx] = nextdp[newx] + ((dp[x] * m1) % MOD);
                    nextdp[newx] %= MOD;
                }
            }
            dp = Arrays.copyOf(nextdp, nextdp.length);
        }
        System.out.println(dp[n]);
    }

    public static long ksm(long a, int p) {
        if (p == 0) {
            return 1l;
        }
        if (p == 1) {
            return a;
        }
        long tmp = ksm(a, p / 2);
        long ans = tmp * tmp;
        ans %= MOD;
        if (p % 2 == 1) {
            ans = ans * a;
            ans %= MOD;
        }
        return ans;
    }

}

 

标签:atcoder,long,Sugoroku,newx,static,ans,nextdp,293,MOD
From: https://www.cnblogs.com/fishcanfly/p/16842798.html

相关文章

  • AtCoder Beginner Contest 275 E F
    E-Sugoroku4题意:从0走到n,有k次操作,每次会丢出一个骰子,骰子上的数字为\([1,m]\),等概率出现问到达n的概率思路:\(dp[i][k]\)表示用了k次操作到达位置i的概......
  • Atcoder Beginner Contest 275(A~F)
    我好菜啊……又没切掉F,40+min切掉A~E成功滚粗。希望能越打越好吧……赛时A求序列最大值,B简单计算,C数正方形,跳过。D递推不太行,N的范围有点大。但是除法的转移......
  • AtCoder Beginner Contest 275
    咕咕咕咕咕咕。G-InfiniteKnapsack做法1-二分假设第\(i\)个物品选了\(x_i\)个,\(x_i\)为非负整数,有\[\lim_{x\to+\infin}\frac{f(x)}{x}=\frac{\sum_ic_......
  • AtCoder Regular Contest 060
    F题有循环节,一看就有KMP,比较难,太晚了,早上起来再补。C-TakandCards\(n\)个整数\(a_1\sima_n\),问有多少种选数方案,使得选出来的数平均值为\(A\)。\(1\len,a_i......
  • AtCoder Regular Contest 059
    EducationalDPRound(确信C-BeTogether给定\(n\)个数\(a_{1}\sima_n\),你至多可以对每个\(a_i\)操作一次,以\((a_i-y)^2\)的代价令\(a_i\getsy\),求\(a\)全......
  • P2939 [USACO09FEB]Revamping Trails G
    题目描述FarmerJohndutifullychecksonthecowseveryday.HetraversessomeoftheM(1<=M<=50,000)trailsconvenientlynumbered1..Mfrompasture1al......
  • Atcoder Grand Contest 003(A~F)
    赛时打了80分钟,后来因为要处理一些私事就没再打,过掉了ABC,推了DE没推出来,不能算很差,但也不算很好。总结一下吧。赛时A一眼题,统计四个方向是否出现过,如果相对的两......
  • AtCoder Beginner Contest 247 E - Max Min
    题目描述简要描述:给定一个长度为\(N\)的数组,求数组的子数组满足最大值为\(X\)且最小值为\(Y\)的子区间的个数。做法1.ST表+二分时间复杂度:\(O(n\logn)\)......
  • AtCoder Beginner Contest 266 Ex Snuke Panic (2D)
    AtCoder传送门洛谷传送门考虑dp,\(f_i\)设在\(t_i\)时刻到达第\(i\)个点,能获得的最大收益。转移:\(f_i=f_j+a_i\),其中\(j\)能转移到\(i\)的充要条件有:\(......
  • AtCoder Beginner Contest 201 题解
    vp情况:过了A到E,F没时间也不会。vp总结:ABC表现可以。D慢了一点,写之前大概考虑清楚每个变量或函数的意义,结构明晰才能更快的写出代码。E花的时间太长,原因......