SequenceCrawling in process...Crawling failedTime limit:3000 MsMemory limit:65536kb64bit Io format:% I64d & % i64u
Submitstatus practice Ural 1528
Description
You are given a recurrent formula for a sequence
F:
F(
N) = 1 +
F(1)
G(1) +
F(2)
G(2) +... +
F(
N? 1)
G(
N? 1), where
GIs also a Recurrent Sequence given by formula
G(
N) = 1 + 2
G(1) + 2
G(2) + 2
G(3) +... + 2
G(
N? 1 )?
G(
N? 1)
G(
N? 1). It is known that
F(1) = 1,
G(1) = 1. Your task is to find
F(
N) Mod
P.
Input
The input consists of several cases. Each case contains two numbers on a single line. These numbers are
N(1 ≤
N≤ 10000) and
P(2 ≤
PLess than or equal to 2 · 109). The input is terminated by the case
N=
P= 0 which shocould not be processed. The number of cases in the input does not exceed 5000.
Output
Output for each case the answer to the task on a separate line.
Sample Input
Input |
Output |
1 22 110 0 |
12 |
Question: for example, question.
Thought: Wow. At first, I read the wrong question. It is not difficult to find the general function of F (n.
AC code:
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <cstring>#include <stdlib.h>using namespace std;int main(){ int n,p; while(~scanf("%d%d",&n,&p)){ if(n==0&&p==0) break; long long ans=1; for(int i=2;i<=n;i++){ ans*=i%p; ans%=p; } printf("%d\n",ans%p); } return 0;}
Zookeeper