第一题!

探讨了zhx需要为他的后辈准备n个不同难度的问题,并希望这些问题的难度能够以一种美丽的方式排列。美丽定义为:存在一个i使得前i个数单调递增或递减,且剩余部分也单调递增或递减。任务是计算所有可能的美丽排列数量,并输出结果对p取模后的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

zhx's contest

As one of the most powerful brushes, zhx is required to give his juniors  n n problems.
zhx thinks the  ith ith problem's difficulty is  i i. He wants to arrange these problems in a beautiful way. 
zhx defines a sequence  {ai} {ai} beautiful if there is an  i i that matches two rules below:
1:  a1..ai a1..ai are monotone decreasing or monotone increasing. 
2:  ai..an ai..an are monotone decreasing or monotone increasing. 
He wants you to tell him that how many permutations of problems are there if the sequence of the problems' difficulty is beautiful. 
zhx knows that the answer may be very huge, and you only need to tell him the answer module  p p.

Input
Multiply test cases(less than  1000 1000). Seek  EOF EOF as the end of the file. 
For each case, there are two integers  n n and  p p separated by a space in a line. ( 1n,p1018 1≤n,p≤1018)
Output
For each test case, output a single line indicating the answer. 
Sample Input
2 233
3 5
Sample Output
2
1

#include <cstdio>
#include <iostream>
long long mod;
using namespace std;
int main()
{
long long n,p,ans;
long long jia(long long a,long long b) ;
long long mul(long long n,long long k);
while(cin>>n>>mod)
{
if(mod==1) cout<<0<<endl;
else if(n==1) cout<<1<<endl;
else 
{
ans=mul(2,n)-2;
if(ans<0) ans+=mod;
cout<<ans<<endl;
}
}
}
long long chen(long long n,long long k) 
{
    long long ans=0;
while(k)
{
if(k%2==1) ans=ans%mod+n%mod;
n=(n%mod)+(n%mod);
k/=2;
}
return ans;
}
long long mul(long long n,long long k)
{
long long ans=1;
while(k)
{
if(k%2==1) ans=chen(ans,n)%mod;
n=chen((n%mod),(n%mod));
k/=2;
}
return ans;
}

今天刚做了这个题目,就感觉很奇怪,为什么我用C++交上去是超时,换成G++就是AC,虽然能AC就好了,但我经常碰到这种怪事,还是希望有人来教教我怎么处理这些环境问题,有人教才是最幸福的啊。

https://vjudge.NET/contest/147891#problem/D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值