Sunday, August 10, 2008

ural1009


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


class Program
{
static void Main(string[] args)
{
problem p = new problem();
}
}

class problem
{
public problem()
{
int n = RTIO.GetInt();
int k = RTIO.GetInt();
long[] d=new long[n+1];
d[1] = k - 1;
d[2] = k * d[1];
for (int i = 3; i <= n; i++)
{
d[i] = (k - 1) * (d[i - 1] + d[i - 2]);
}
Console.WriteLine(d[n]);
}
}

class RTIO
{
static string bufferStr = "";
public static bool isMaskHave(ref int mask, ref int j)
{
return (mask & (1 << j)) > 0;
}
public static int GetInt()
{
if (bufferStr.Length == 0)
{
bufferStr = Console.ReadLine();
}
int index = bufferStr.IndexOf(' ');
if (index < 0) index = bufferStr.Length;

int res = int.Parse(bufferStr.Substring(0, index));
if (index != bufferStr.Length)
{
bufferStr = bufferStr.Remove(0, index + 1);
}
else
{
bufferStr = "";
}
return res;
}
public static string GetString()
{
if (bufferStr.Length == 0)
{
bufferStr = Console.ReadLine();
}
string res = bufferStr;
bufferStr = "";
return res;
}
}

class RTUtil
{
public static int GetSum(int[] array)
{
int res = 0;
for (int i = 0; i < array.Length; i++)
{
res += array[i];
}
return res;
}
}

No comments: