SGU 407 Number of Paths in the Empire dp+java大数

SGU 407

407. Number of Paths in the Empire

Time limit per test: 0.75 second(s)

Memory limit: 65536 kilobytes

input: standard

output: standard

During the period of Tsam dynasty ruling people rarely fought against each other and their neighbours, because they just could not afford themselves such
a waste of time. The matter for it is that they were entirely absorbed with solving various problems which were connected with trade, craft, agriculture and other spheres of human activity. In a wide list of problems the ones of tax collection stand out. As
one of such problems was posed by Emperor himself, it was of great importance. The problem was to count the number of different paths consisting of exactly m roads.
Every path should have started and ended in the capital of Empire. Paths were supposed to cover the same towns and roads any times, moreover they could cover the capital several times. Now you are to solve this problem given information about Empire: there
were n country towns situated
at the foot of a hill, they formed a circle at the bottom, and the capital was on the top of the hill. The capital was connected to all other towns, and each country town was also connected to other two neighbouring country towns both to the left and to the
right.  Pic.
1 Empire comprising the capital (index 0) and four country towns (indices 1 — 4).

Input

The only line of input file contains two integers n and m (3
≤ n ≤ 1000, 0 ≤ m ≤
5000).

Output

Output the answer without leading zeros.

Example(s)

sample input
sample output
4 3
8
sample input
sample output
3 4
21

Commentary to the first sample test. There are 8 paths in the Empire. 0-1-2-0, 0-2-3-0, 0-3-4-0, 0-4-1-0, 0-2-1-0, 0-3-2-0, 0-4-3-0, 0-1-4-0.

dp方程还是很好写的,,然后来个大数,本地跑不出极限数据一直没交,实在无解了才交了一发,居然过了。。

==

import java.io.*;
import java.util.*;
import java.math.*;
public class Solution {
	BigInteger[][] dp = new BigInteger[5005][2];
	public void work() {
		int n, m;
		n = cin.nextInt();
		m = cin.nextInt();
		dp[0][0] = BigInteger.ONE;
		dp[0][1] = BigInteger.ZERO;
		for(int i = 1; i <= m ; i++) {
			dp[i][0] = dp[i-1][1];
			BigInteger tmp = dp[i-1][0].multiply(BigInteger.valueOf(n));
			dp[i][1] = tmp.add(dp[i-1][1].multiply(BigInteger.valueOf(2)));
		}
		out.println(dp[m][0]);
		//out.println('*');
		out.close();
	}

	Solution() {
		cin = new Scanner(System.in);
		out = new PrintWriter(System.out);
	}

	public static void main(String[] args) {
		Solution e = new Solution();
		e.work();
	}

	public Scanner cin;
	public PrintWriter out;

}

SGU 407 Number of Paths in the Empire dp+java大数

时间: 2024-10-19 05:52:29

SGU 407 Number of Paths in the Empire dp+java大数的相关文章

hdu 3006 The Number of set(思维+壮压DP)

The Number of set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1056    Accepted Submission(s): 655 Problem Description Given you n sets.All positive integers in sets are not less than 1 and

sgu 143 Long live the Queen 简单树形dp

// sgu 143  Long live the Queen 简单树形dp // // 题意:在树上选一个连通字图,使得节点的权值之和最大 // f[i] 表示以该节点为根的字图权值之和的最大值 // 则有 f[i] = w[i] + sigma(max(0,f[j])) i是j的父节点 // 最后在所有的f中挑个最大值就是答案.... #include <algorithm> #include <bitset> #include <cassert> #include

【leetcode】1301. Number of Paths with Max Score

题目如下: You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character 'S'. You need to reach the top left square marked with the character 'E'. The rest of the squares are labeled ei

1301. Number of Paths with Max Score

You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character 'S'. You need to reach the top left square marked with the character 'E'. The rest of the squares are labeled either w

Numbering Paths (Uva 125 floyd+dp思想)

Numbering Paths Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Problems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decis

UVa 10564 - Paths through the Hourglass(DP)

Description Problem F Paths through the Hourglass Input: Standard Input Output: Standard Output Time Limit: 2 Seconds In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each cell in the pat

UVA 10564 Paths through the Hourglass[DP 打印]

UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径. f[i][j][k]从下往上到第i层第j个和为k的方案数 上下转移不一样,分开处理 没必要判断走出沙漏 打印方案倒着找下去行了,尽量往左走 沙茶的忘注释掉文件WA好多次 #include <iostream> #include <cstdio> #include <algor

SGU - 134 Centroid 无根树转有根树 + 树形DP

题目大意:给出一个无向图(树),要求你删除掉其中一个点,使剩下的点构成的子树中,节点数最大的那个值达到最小 解题思路:因为给出的是一个无根树,第一个想法就是先把它转成有根树,将1当成根 设sum[i]为以i为根节点的子树有多少个节点,那么sum[1] - sum[i]就相当于是排除了i的所有子节点的另一棵子树的节点总数了 设dp[i]为去掉了i节点后的剩余节点所构成的子树的节点的最大值 那么dp[i] = max(dp[i], sum[son]) son指的是和i相连的子节点 还有另一棵子树,就

SGU 143 Long Live the Queen (树形DP)

143. Long Live the Queen time limit per test: 0.25 sec. memory limit per test: 4096 KB The Queen of Byteland is very loved by her people. In order to show her their love, the Bytelanders have decided to conquer a new country which will be named accor