URL 1083. Factorials !!! (Reading Comprehension)
1083. Factorials !!! Time limit: 1.0 second
Memory limit: 64 MB
Definition 1.
N!!...! =
N(
N?
K)(
N? 2
K)... (
NMod
K), If
KDoesn' t divide
N;
N!!...! =
N(
N?
K)(
N? 2
K)...
K, If
KDivides
N(There are
KMarks! In the both cases ).
Definition 2.
XMod
Y-A remainder after division
XBy
Y. For example, 10 mod 3 = 1; 3! = 3 · 2 · 1; 10 !!! = 10 · 7 · 4 · 1. Given numbers
NAnd
KWe have calculated a value of the expression in the first definition. Can you do it as well? Inputcontains the only line: one integer
N, 1 ≤
N≤ 10, then exactly one space, then
KExclamation marks, 1 ≤
K≤ 20. Outputcontains one number-
N!!...! (There are
KMarks! Here). Sample
Problem Author:Oleg Katz
Problem Source:The 3rd high school children programming contest, USU, Yekaterinburg, Russia, March 4, 2001
Resolution: calculate the value based on the definition in the question.
AC code:
#include
using namespace std;int main(){ int n; string s; while(~scanf("%d", &n)){ cin>>s; int k = s.size(); int ans = 1; if(n % k){ int t = n / k; for(int i=0; i<=t; i++){ ans *= (n - i * k); } } else{ int t = n / k - 1; for(int i=0; i<=t; i++){ ans *= (n - i * k); } } printf("%d\n", ans); } return 0;}