ural 1009. K-based Numbers dp 高精度

点击打开链接

1009. K-based Numbers

Time limit: 1.0 second

Memory limit: 64 MB

Let’s consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-based notation doesn’t contain two successive zeros. For example:

  • 1010230 is a valid 7-digit number;
  • 1000198 is not a valid number;
  • 0001235 is not a 7-digit number, it is a 4-digit number.

Given two numbers N and K, you are to calculate an amount of valid K based numbers, containing Ndigits.

You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 18.

Input

The numbers N and K in decimal notation separated by the line break.

Output

The result in decimal notation.

Sample

input output
2
10
90

Problem Source: USU Championship 1997

dp[i][0]代表第i位不存在0的情况,dp[i][1]代表第i位存在1的情况。

dp[i][1]=dp[i-1][0]表示在前i-1个数中最后一个数不是0 的情况补0

dp[i][0]=(k-1)*(dp[i-1][0]+dp[i-1][1])表示在前i-1位中无论最后一位是不是0都补1~9的情况

//0.125	3 854 KB
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);// 输入
		BigInteger dp[][] = new BigInteger[2007][2];
		int n, k;
		n = cin.nextInt();
		k = cin.nextInt();
		dp[1][0] = (BigInteger.valueOf(k - 1));
		dp[1][1] = (BigInteger.valueOf(0));
		for (int i = 2; i <= n; i++) {
			dp[i][0] = (dp[i - 1][0].add(dp[i-1][1])).multiply(BigInteger.valueOf(k-1));
			dp[i][1] = (dp[i - 1][0]);
		}
		System.out.println(dp[n][0].add(dp[n][1]));
	}
}

时间: 2024-11-11 02:31:21

ural 1009. K-based Numbers dp 高精度的相关文章

URAL - 1009 - K-based Numbers (简单DP)

1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-based notation doesn't contain two successive zeros. For example: 1010230 is a valid

ural 1009 K-based Numbers

 1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-based notation doesn't contain two successive zeros. For example: 1010230 is a va

URAL 1036(dp+高精度)

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1036 Description You are given a number 1 ≤ N ≤ 50. Every ticket has its 2 N-digit number. We call a ticket lucky, if the sum of its first N digi

DP+高精度 URAL 1036 Lucky Tickets

题目传送门 1 /* 2 题意:转换就是求n位数字,总和为s/2的方案数 3 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 4 高精度直接拿JayYe的:) 5 异或运算的规则: 6 0⊕0=0,0⊕1=1 7 1⊕0=1,1⊕1=0 8 口诀:相同取0,相异取1 9 */ 10 #include <cstdio> 11 #include <cstring> 12 #include <string>

Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description A group of k students from Cooking University living in the campus decided that each day of the semester one of them will p

poj 1625 Censored!(AC自动机+DP+高精度)

题目链接:poj 1625 Censored! 题目大意:给定N,M,K,然后给定一个N字符的字符集和,现在要用这些字符组成一个长度为M的字符串,要求不包 括K个子字符串. 解题思路:AC自动机+DP+高精度.这题恶心的要死,给定的不能匹配字符串里面有负数的字符情况,也算是涨姿势 了,对应每个字符固定偏移128单位. #include <cstdio> #include <cstring> #include <queue> #include <vector>

URAL 1167. Bicolored Horses (DP)

题目链接 题意 :农夫每天都会放马出去,然后晚上把马赶入马厩,于是让马排成一行入马厩,但是不想马走更多的路,所以让前p1匹入第一个马厩,p2匹马入第二个马厩…………但是他不想让他的任何一个马厩空着,所有的马都必须入马厩.有两种颜色的马,如果 i 匹黑马与 j 匹白马同在一个马厩,不愉快系数是 i * j,总系数就是k个系数相加.让总系数最小. 思路 : dp[i][j] 代表的是前 i 个马厩放 j 匹马的最小不愉快系数值. 1 //1167 2 #include <cstdio> 3 #in

UVA 12105 - Bigger is Better(DP+高精度)

题目链接:12105 - Bigger is Better 题意:一些火柴,问你能组成整除m最大的数字是多少. 思路:dp[i][j]表示用i根火柴,组成%m余数为j的最大数字,末尾多一个数字k的状态就是dp[i + num[k]][(j * 10 + k) % m],由于最多可能50位数,所以要用高精度. 注意一个优化点,由于高精度的计算上只需要乘10+k,常规的高精度乘法复杂度还是有点高会超时,所以用数组去模拟,每次*10 + k的时候就往后多一位即可. 代码: #include <stdi

Ural 1183 Brackets Sequence(区间DP+记忆化搜索)

题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0<=i<=j<len (len为输入序列的长度). c[i][j]为输入序列从下标i到下标j的断开位置.假设没有断开则为-1. 当i==j时.d[i][j]为1 当s[i]=='(' && s[j]==')' 或者 s[i]=='[' && s[j]==']'时,d