HDU – 5187 – zhx's contest (高速幂+高速乘)

HDU – 5187 – zhx's contest (高速幂+高速乘)

大家好,又见面了,我是全栈君。

zhx’s contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 448    Accepted Submission(s): 147




Problem Description
As one of the most powerful brushes, zhx is required to give his juniors 



n
 problems.

zhx thinks the 



ith
 problem’s difficulty is 



i
. He wants to arrange these problems in a beautiful way.

zhx defines a sequence 



{ai}
 beautiful if there is an 



i
 that matches two rules below:

1: 



a1..ai
 are monotone decreasing or monotone increasing.

2: 



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
.
 


Input
Multiply test cases(less than 



1000
). Seek 



EOF
 as the end of the file.

For each case, there are two integers 



n
 and 



p
 separated by a space in a line. (



1n,p1018
)
 


Output
For each test case, output a single line indicating the answer.

 


Sample Input
   
   
2 233 3 5

 


Sample Output
   
   
2 1
Hint
In the first case, both sequence {1, 2} and {2, 1} are legal. In the second case, sequence {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} are legal, so the answer is 6 mod 5 = 1

 


Source
 

思路:由题意能够求出答案为(2^n-2)%p


可是n。p都是LL型的,高速幂的时候会爆LL,所以这里要用到高速乘法,高速乘法事实上和高速幂差点儿相同。就是把乘号改为加号


注意:当n为1时。要输出1,而当p为1时要输出0。


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long 
using namespace std;

LL n, p;

LL multi(LL a, LL b) {	//高速乘法。事实上和高速幂差点儿相同 
    LL ret = 0;
    while(b) {
        if(b & 1) ret = (ret + a) % p;
        a = (a + a) % p;
        b >>= 1;
    }
    return ret;
}

LL powmod(LL a, LL b) {	//高速幂 
    LL ret = 1;
    while(b) {
        if(b & 1) ret = multi(ret, a) % p;
        a = multi(a, a) % p;
        b >>= 1;
    }
    return ret;
}


int main() {
	while(cin >> n >> p) {
		if(p == 1) {
			cout << 0 << endl;
		} else if(n == 1) {
			cout << 1 << endl;
		} else {
			LL ans = powmod(2, n) - 2;
			if(ans < 0) ans += p;
			cout << ans << endl;
		}
	}
	return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115513.html原文链接:https://javaforall.net

(0)
上一篇 2022年2月3日 下午1:00
下一篇 2022年2月3日 下午1:00


相关推荐

  • 前端页面小图标不显示问题

    前端页面小图标不显示问题这个问题困扰了我好久 主要报错是 Origin http localhost isthereforen 等 起因是我引入 bootstrap 框架后后端页面的一些小图标不显示 我起初是觉得可能我的路径不对 改了两遍后还是不好使 就怀疑是编译器没有吧全部路径找全 然后就自己一点点的重新修改 ps 你能想象 1000 多行的程序一行行的看博主是有多

    2026年3月16日
    2
  • pycharm配置flask环境_调试是什么意思

    pycharm配置flask环境_调试是什么意思1.Flask的调试模式​ 通过调用run()方法启动Flask应用程序。但是,当应用程序正在开发中时,应该为代码中的每个更改手动重新启动它。为避免这种不便,请启用调试支持。如果代码更改,服务器将自行重新加载。它还将提供一个有用的调试器来跟踪应用程序中的错误(如果有的话)。在运行或将调试参数传递给run()方法之前,通过将application对象的debug属性设置为True来启用Debug模式。app.debug=Trueapp.run(debug=True)但是在pycharm编译器

    2022年8月29日
    7
  • 因子分析(FA)算法简述

    因子分析(FA)算法简述文章目录前言一 什么是因子分析 1 1 因子分析应用背景 1 2 因子分析算法的基本步骤 1 3 因子分析算法的数学解释 1 3 1 因子模型 1 3 2 因子载荷矩阵的求解二 因子分析的应用实例三 主成分分析 PCA 与因子分析 FA 的联系与区别总结前言在学习数据降维时 了解到因子分析 FA 算法是其中的一种方式 因此 在这里对因子分析算法做一个简要的归纳 梳理 后续会对数据降维的几种方式做个总结 感兴趣的朋友 可以持续关注 一 什么是因子分析 因子分析法是指 研究从变量群中提取共性因子的统计技术

    2026年3月18日
    2
  • 余弦定理的证明及其应用

    余弦定理的证明及其应用余弦定理余弦定理 顾名思义 与余弦函数 cos 有关 具体的是这样的对于任意一个三角形 ABC 有如下结论 a2 b2 c2 2bc cosAb2 a2 c2 2ac cosBc2 a2 b2 2ab cosC 为什么呢 余弦定理的证明在上面那张图中其实大家就能看到证明方法根据三角函数 BD c cosB AD c sinB 那么 DC a c cosB 接下来 根据勾股定理 我们就可以得

    2026年3月16日
    2
  • Jlink或者stlink用于SWD接口下载程序[通俗易懂]

    Jlink或者stlink用于SWD接口下载程序[通俗易懂]最近要使用stm32f103c8t6最小系统板,直接ISP串口下载程序太麻烦,就想着使用swd接口来调试。结果:通过SWD接口下载程序成功,但调试失败,还不知原因,会的的人麻烦交流一下。SWD接口:3.3VDIO(数据)CLK(时钟)GND1.首先声明jlink和stlink都有jtag和swd调试功能。jlink接口如下:如图,我使用的就是VCC…

    2022年4月25日
    62
  • 四种多线程同步机制

    四种多线程同步机制参考一 1 nbsp 临界区 CriticalSect 因为 CriticalSect 不是内核对象 所以只能用来同一进程内线程间的同步 不能用来多个不同进程间的线程的同步 2 如果在 CriticalSect 中间突然程序 crash 或是 exit 而没有调用 LeaveCritica 则结果是该线程所对应的内核不能被释放 该线程成为死线程 3 要比其他

    2026年3月18日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号