URAL 1528 Sequence

SequenceCrawling in process...
Crawling failed
Time Limit:3000MS    
Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit
Status 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 + 2g(1) + 2g(2) + 2g(3) + … + 2g(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 2
2 11
0 0
1
2

题意:如题。

思路:哇。一开始看错题啦。不难发现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;
}



URAL 1528 Sequence,布布扣,bubuko.com

时间: 2024-11-03 22:18:24

URAL 1528 Sequence的相关文章

URAL 1306 Sequence Median(优先队列)

题意:求一串数字里的中位数.内存为1M.每个数范围是0到2的31次方-1. 思路:很容易想到把数字全部读入,然后排序,但是会超内存.用计数排序但是数又太大.由于我们只需要第n/2.n/2+1大(n为偶数)或第(n+1)/2大(n为奇数).所以可以用优先队列来维护最值,这样只需要存一半元素(n/2+1个元素)就可以了. #include<cstdio> #include<algorithm> #include<queue> #define UL unsigned int

ural 1306. Sequence Median

1306. Sequence Median Time limit: 1.0 secondMemory limit: 1 MBLanguage limit: C, C++, Pascal Given a sequence of N nonnegative integers. Let's define the median of such sequence. If N is odd the median is the element with stands in the middle of the

URAL 1306 - Sequence Median 小内存求中位数

[题意]给出n(1~250000)个数(int以内),求中位数 [题解]一开始直接sort,发现MLE,才发现内存限制1024k,那么就不能开int[250000]的数组了(4*250000=1,000,000大约就是1M内存). 后来发现可以使用长度为n/2+1的优先队列,即包含前一半的数以及中位数,其他数在读入的时候就剔除,这样可以省一半的空间. 1 #include<bits/stdc++.h> 2 #define eps 1e-9 3 #define FOR(i,j,k) for(in

ural Brackets Sequence (dp)

http://acm.timus.ru/problem.aspx?space=1&num=1183 很经典的问题吧,看的黑书上的讲解. 设dp[i][j]表示i到j括号合法需要的最少括号数. 共有四种情况: s[i]s[j]配对,dp[i][j] = min( dp[i][j] ,  dp[i-1][j+1] ): s[i] = '('或'[' dp[i][j] = min( dp[i][j] , dp[i+1][j]+1 ): s[j] = ')'或']' dp[i][j] = min( dp

ural 1219. Symbolic Sequence

1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequence of 1 000 000 lowercase Latin letters. This sequence should satisfy the following restrictions: Every letter occurs not more than 40 000 times in th

ural 1247. Check a Sequence

1247. Check a Sequence Time limit: 0.5 secondMemory limit: 64 MB There is a sequence of integer numbers A1, A2, …, AS, and a positive integer N. It's known that all elements of the sequence {Ai} satisfy the restriction 0 ≤ Ai ≤ 100. Moreover, it's kn

递推DP URAL 1081 Binary Lexicographic Sequence

题目传送门 1 /* 2 dp[i][1]/dp[i][0] 表示从左往右前i个,当前第i个放1或0的方案数 3 k -= dp[n][0] 表示当前放0的方案数不够了,所以必须放1,那么dp[n][0]个方案数都不能用了 4 相当于k减去这么多 5 详细解释:http://www.cnblogs.com/scau20110726/archive/2013/02/05/2892587.html 6 */ 7 #include <cstdio> 8 #include <algorithm&

记忆化搜索+DFS URAL 1183 Brackets Sequence

题目传送门 1 /* 2 记忆化搜索+DFS:dp[i][j] 表示第i到第j个字符,最少要加多少个括号 3 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 4 当s[x] 与 s[y] 匹配,则搜索 (x+1, y-1); 否则在x~y-1枚举找到相匹配的括号,更新最小值 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include

URAL 1081 Binary Lexicographic Sequence

第13个位置第5个Bit :13>num[4] =>1 第四个bit 13-num[4]=5 :5<num[3] =>0 ,3-1 第三个Bit 5>num[2](3) 5-num[2]=2 ... #include<stdio.h> int num[45]; void init() { num[0]=1; num[1]=2; int k=2; while(k<44) { num[k]=num[k-1]+num[k-2]; k++; } } int main