Ural 1528 Sequence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.