【HDU - 5187】zhx's contest (快速幂+ 快速乘,模板)

题干:

2018百度之星复赛晋级名单出炉(增加20%晋级名额)~

zhx's contest

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


 

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. (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

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

BestCoder Round #33

 

解题报告:

      首先这题找规律,题目推出来通式是:2^n - 2。证明如下:

推的过程就是一共有四种情况: 升升,升降,降升,降降,其中升升和降降最简单,一共有两种,复杂的就是升降和降升这两种情况,首先来看降生,那么ai一定是最小值,因为两边都算ai了,所有当在第一个空的时候,前面一共有Cn-11, 后面就自动的确定了,在第二位的时候,有Cn-12, 同理到最后Cn-1n-2,所以加起来就是2n-1-2,这是降升,同理升降也是这么多,所以最后结果就是(2n-1-2) * 2 + 2 = 2n-2;

      注意的是这题的乘法不能直接乘法,因为longlong * longlong可能会爆掉,所以这里用快速乘法,把longlong * longlong转化成longlong + longlong 去做。

AC代码:

#include<iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long LL;LL qmul(LL a, LL k, LL mod) { //快速乘法LL ans = 0;//加法的幺元while (k) {if (k & 1) ans = (ans + a)%mod;a = (a + a) % mod;//和快速幂一样,只不过这里是加k >>= 1;}return ans;
}
LL qpow(LL a, LL k, LL mod) { //快速幂LL ans = 1;while (k) {if (k & 1) ans = qmul(ans, a, mod);//不能直接乘a = qmul(a, a, mod);k >>= 1;}return ans;
}int main() {LL n, p;while (~scanf("%I64d %I64d", &n, &p)) {if (n == 1) { //特判一下printf("%I64d\n", 1 % p);continue;}printf("%I64d\n", (qpow(2, n, p) - 2 + p) % p);//这一步注意,不要为负数}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441139.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python批量打印word_Python使用扩展库pywin32实现批量文档打印实例

本文代码需要正确安装Python扩展库pywin32&#xff0c;建议下载whl文件进行离线安装。然后调用win32api的ShellExecute()函数来实现文档打印&#xff0c;系统会根据文档类型自动选择不同的软件进行打开并自动打印&#xff0c;如果要打印的是图片的话&#xff0c;需要手工确认一…

【牛客 - 283E】贪心只能过样例(模拟)

题干&#xff1a; 小西是单身狗&#xff0c;所以她不喜欢看到有CP在秀恩爱&#xff01; 有一天&#xff0c;小西出来闲逛&#xff0c;发现街上的行人都排成了一排&#xff0c;并且可以用这种形式表示&#xff1a; MMFMMFFFMMM 其中M表示男孩子&#xff0c;F表示女装的男孩…

apmserver导入MySQL_mysql数据库导入导出

window下1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname > dbname.sql2.导出一个表mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名mysqldump -u dbuser -p dbname users> dbname_users.sql3.导出一个数据库结…

【牛客 - 283C】模拟只会猜题意(简单模拟)

题干&#xff1a; 小西突然觉得回文串是一种非常优雅的东西&#xff0c;她突然想要把身边所有的字符串都变成回文&#xff01; 所谓回文串就是一个倒置后仍与自身相等的字符串&#xff0c;如“gxuacmmcauxg”和“gxuacmcauxg”。 小西不喜欢单身狗&#xff0c;所以小西只会…

sql开启mysql远程连接_SQLServer2008设置开启远程连接

SQLServer2008设置开启INTERNET远程连接 SQL Server 2008默认是不允许远程连接的&#xff0c;sa帐户默认禁用的&#xff0c;如果想要在本地用SSMS连接远程服务器上的SQL Server 2008&#xff0c;需要做两个部分的配置&#xff1a; 使用sa账户登录SQL Server Management Studio(…

【牛客 - 283H】图论一顿套模板(思维转化,Dijkstra)

题干&#xff1a; 由于临近广西大学建校90周年校庆&#xff0c;西大开始了喜闻乐见的校园修缮工程&#xff01; 然后问题出现了&#xff0c;西大内部有许许多多的道路&#xff0c;据统计有N栋楼和M条道路&#xff08;单向&#xff09;&#xff0c;每条路都有“不整洁度”W&…

spss相关性分析看结果_spss相关性分析

当我们想要了解变量的相关程度时,就需要用到相关分析,而相关分析也是回归之前很重要的一步,通常用到的方法是pearson方法。 首先解释一下相关系数,相关系数反应的是两个变量之间变化趋势的方向以及程度,其值范围为-1到+1,正值表示正相关,负值表示负相关,绝对值越大表示…

【牛客 - 283F】出装方案(最小费用最大流)

题干&#xff1a; 众所周知&#xff0c;在各种对抗类游戏里装备都是很重要的一环&#xff0c;不同的出装方案会给玩家带来不同的强度。 dalao手里有N件装备&#xff0c;现在dalao要把装备分给N个队友&#xff0c;每个队友只能分一件装备&#xff0c;而每个队友穿上不同的装…

mysql瓶颈分析_网站瓶颈分析—MYSQL性能分析

一、关于慢查询设置和分析查找慢查询参数mysql> show variables like long%;---------------------------| Variable_name | Value |---------------------------| long_query_time | 10.000000 |---------------------------mysql> show variables like slow%;---------…

【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)

题干&#xff1a; Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wanted to find a string that meets the following conditions: The string consists of n lowercase English letters (that is, the strings length e…

【CodeForces - 289D】Polo the Penguin and Houses (带标号的无根树,Cayley定理,Prufer编码)

题干&#xff1a; Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n). Little penguin Po…

离散信号的抽取和内插例题_《数字信号处理》学习指导与题解 2011年版

《数字信号处理》学习指导与题解出版时间&#xff1a;2011年版内容简介《〈数字信号处理〉学习指导与题解》对“数字信号处理”教学大纲要求的所有知识点进行了提纲挈领的阐述&#xff0c;对于教材《数字信号处理》(吴瑛等主编&#xff0c;2009年8月由西安电子科技大学 出版发行…

【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心)

题干&#xff1a; Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p  p0, p1, ..., pn, Polo has defined its beauty — number . Expression means applying the operation …

龙果学院mysql分布式集群代码_龙果学院-MySQL大型分布式集群解决方案

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼1 课程概述2 课程背景3 纵观大型网站架构发展&#xff0c;总结持久化部分需要应对的问题4 操作系统安装以及配置5 在CentOS上通过yum安装mysql5.76 mysql初次见面-mysql5.7的用户以及安全策略7 mysql初次见面续-mysql基本操作8 认识…

【SPOJ - TOURS 387】Travelling tours (最小费用最大流,拆点)

题干&#xff1a; In Hanoi, there are N beauty-spots (2 < N < 200), connected by M one-way streets. The length of each street does not exceed 10000. You are the director of a travel agency, and you want to create some tours around the city which sati…

python问题化教学设计_基于IPO的Python教学设计

冯艳茹 陈平摘要&#xff1a;程序设计基础课程是培养大学生解决计算问题的思维和能力的课程&#xff0c;使用Python作为大学生的首门编程语言课程&#xff0c;可操作性强&#xff0c;入门容易&#xff0c;上手快。该文提出了基于IPO的教学设计新思维&#xff0c;使教学活动和教…

【SPOJ - SCITIES】Selfish Cities (二分图最优匹配,最大费用流)

题干&#xff1a; Far, far away there is a world known as Selfishland because of the nature of its inhabitants. Hard times have forced the cities of Selfishland to exchange goods among each other. C1 cities are willing to sell some goods and the other C2 c…

将方孔分段的lisp_AutoLisp编写工程地质剖面图小工具

AutoLisp编写工程地质剖面图小工具朱红雷李健民 (浙江省水利水电勘测设计院杭州 310002)在我院应用的CAD工程地质制图系统中&#xff0c;通常采用的各种高级语言编制的程序&#xff0c;一般是通过编制数据文件&#xff0c;生成CAD图形数据交换文件(一般为*.SCR或*.DXF)达到绘制…

【CodeForces - 922B 】Magic Forest (数学,异或,暴力,水题,三元组问题)

题干&#xff1a; Imp is in a magic forest, where xorangles grow (wut?) A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the …

【牛客 -2A】矩阵(二分,字符串哈希)

题干&#xff1a; 给出一个n * m的矩阵。让你从中发现一个最大的正方形。使得这样子的正方形在矩阵中出现了至少两次。输出最大正方形的边长。 输入描述: 第一行两个整数n, m代表矩阵的长和宽&#xff1b; 接下来n行&#xff0c;每行m个字符&#xff08;小写字母&#xff09…