URAL 1528 Sequence

來源:互聯網
上載者:User

標籤:des   style   http   color   os   strong   io   width   

SequenceCrawling in process...Crawling failedTime Limit:3000MS    Memory Limit:65536KB     64bit 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 g is 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 ≤  p ≤ 2·109). The input is terminated by the case with n = p = 0 which should 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

題意:如題。

思路:哇。一開始看錯題啦。不難發現f(n)的通項啦。

AC代碼:

#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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.