URAL 1528 Sequence

Description

You are given a recurrent formula for a sequence  f:
fn) = 1 +  f(1)  g(1) +  f(2)  g(2) + … +  fn−1)  gn−1),
where  g is also a recurrent sequence given by formula
gn) = 1 + 2  g(1) + 2  g(2) + 2  g(3) + … + 2  gn−1) −  gn−1)  gn−1).
It is known that  f(1) = 1,  g(1) = 1. Your task is to find  fn) 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·10  9). 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

题目在吓唬人啊,仔细算一算这个就是阶乘啊。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	int n, p;
	while (cin >> n >> p, n, p)
	{
		long long tot = 1;
		for (int i = 1; i <= n; i++) tot = tot*i%p;
		cout << tot << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值