A luggage lock has n combination wheels side by side. Each wheel carries k different symbols, and every wheel turns on its own. The symbols set on the n wheels, read from the left, form one combination; two combinations are different if even one wheel shows a different symbol. The first line gives k and n separated by a single space. On the first line, print the number of combinations this lock can have. The owner of the bag remembers only the symbol of the leftmost wheel. On the second line, print the number of combinations still possible once the leftmost wheel is set to that symbol. If there is only one wheel (n = 1), the number on the second line is 1. Print the numbers with digits only, without commas.
Input
2 ≤ k ≤ 36, 1 ≤ n ≤ 30, k^n ≤ 1,073,741,824 (2^30)
Output
First line: the number of combinations; second line: the number after the leftmost wheel is set
Example 1 · stdin
10 3
stdout
1000
100
Example 2 · stdin
7 1
stdout
7
1
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
This problem takes all 8 languages. Each wheel offers k choices, so there are k^n combinations (k multiplied n times), and once the leftmost wheel is set, k^(n−1) remain. The Python, JS and TS solutions write k ** n directly with the power operator **; the Java, C#, C and C++ solutions use a small function that starts at 1 and multiplies by k e times. Craykon has neither a power operator nor a Mathf function for it (** and ^ are compile errors), so its solution writes the same function and repeats multiplication. Trap one is ^. In Python, JS, TS, C, C++, Java and C#, ^ is bitwise XOR, not a power, so computing example 1 as k ^ n and k ^ (n - 1) gives 9 and 8 instead of 1000 and 100. Trap two is a power function that returns a floating-point number. Java Math.pow and Python math.pow return a double (float), so printing it directly turns example 1 into 1000.0 and 100.0. C++ pow also returns a double, and cout prints doubles with 6 significant digits: 1000 comes out unchanged, but 2^30 becomes 1.07374e+09. C# Math.Pow returns a double too, but Console.WriteLine prints a value with no fractional part as 1000, so it happens to be right in this range. Python's ** and built-in pow return an integer when both arguments are integers. Trap three is the exponent 0. Starting at k and multiplying e−1 more times is right for e ≥ 1, but with n = 1 as in example 2 the second line becomes 7 instead of 1. Starting at 1 and multiplying e times leaves 1 when e = 0, because nothing is multiplied. k^n is at most 2^30, so it fits in an int in every language.
Pythonmain.py · 5 lines
k, n = map(int, input().split())# 바퀴마다 k가지씩 고르니 번호는 k를 n번 곱한 만큼print(k ** n)# 맨 왼쪽 바퀴가 정해지면 남은 바퀴는 n - 1개 (n = 1이면 k ** 0 = 1)print(k ** (n - 1))
JavaScriptmain.js · 5 lines
const [k, n] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// 바퀴마다 k가지씩 고르니 번호는 k를 n번 곱한 만큼console.log(k ** n);// 맨 왼쪽 바퀴가 정해지면 남은 바퀴는 n - 1개 (n = 1이면 k ** 0 = 1)console.log(k ** (n - 1));
TypeScriptmain.ts · 6 lines
import { readFileSync } from "node:fs";const [k, n]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// 바퀴마다 k가지씩 고르니 번호는 k를 n번 곱한 만큼console.log(k ** n);// 맨 왼쪽 바퀴가 정해지면 남은 바퀴는 n - 1개 (n = 1이면 k ** 0 = 1)console.log(k ** (n - 1));
JavaMain.java · 18 lines
import java.util.Scanner;public class Main { // k를 e번 곱한다. e = 0이면 곱하지 않으니 1 static int power(int k, int e) { int result = 1; for (int i = 0; i < e; i++) result *= k; return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); int n = sc.nextInt(); System.out.println(power(k, n)); System.out.println(power(k, n - 1)); }}
C#Main.cs · 20 lines
using System;class Program{ // k를 e번 곱한다. e = 0이면 곱하지 않으니 1 static int Power(int k, int e) { int result = 1; for (int i = 0; i < e; i++) result *= k; return result; } static void Main() { string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int k = int.Parse(t[0]), n = int.Parse(t[1]); Console.WriteLine(Power(k, n)); Console.WriteLine(Power(k, n - 1)); }}
C++main.cpp · 16 lines
#include <iostream>using namespace std;// k를 e번 곱한다. e = 0이면 곱하지 않으니 1int power(int k, int e) { int result = 1; for (int i = 0; i < e; i++) result *= k; return result;}int main() { int k, n; cin >> k >> n; cout << power(k, n) << '\n' << power(k, n - 1) << '\n'; return 0;}
Cmain.c · 15 lines
#include <stdio.h>/* k를 e번 곱한다. e = 0이면 곱하지 않으니 1 */int power(int k, int e) { int result = 1; for (int i = 0; i < e; i++) result *= k; return result;}int main(void) { int k, n; scanf("%d %d", &k, &n); printf("%d\n%d\n", power(k, n), power(k, n - 1)); return 0;}
Craykonmain.cray · 16 lines
// 크레이콘에는 거듭제곱이 없어서 k를 e번 곱한다. e = 0이면 1거듭(k, e){ 결과 = 1; for (i = 0; i < e; i++) { 결과 *= k; } return 결과;}수들 = Console.ReadNumbers();k = 수들[0];n = 수들[1];Console.WriteLine(거듭(k, n));Console.WriteLine(거듭(k, n - 1));