hdu5187_zhx's contest(快速幂+快速乘法)
来源:互联网 发布:mac unix 编辑:程序博客网 时间:2024/06/06 09:31
还是第一次用快速乘法呢,以前知道快速幂的道理,做BC的时候用同样的道理以加法改了一下乘法,后来一查原来有叫“快速乘法”的。
Problem Description
As one of the most powerful brushes, zhx is required to give his juniorsn problems.
zhx thinks theith 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 modulep .
zhx thinks the
zhx defines a sequence
1:
2:
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
Input
Multiply test cases(less than 1000 ). Seek EOF as the end of the file.
For each case, there are two integersn and p separated by a space in a line. (1≤n,p≤1018 )
For each case, there are two integers
Output
For each test case, output a single line indicating the answer.
Sample Input
Sample Input
2 2333 5
Sample Output
21
题目的意思是秋满足这两个条件
1:
2:
的序列的数量。
就是以那个ai为分水岭,左边和右边都分别是单调增或单调减如图
就这四种情况,其中头两种总共就是两个序列,也就是从头到尾递增和从头到尾递减。
后两种方式就是把序列中德数分为左右两派,分完以后左右两边各自内部的排法就已经确定了,至于ai早就确定了(不是全局最大就是全局最小),而除了ai的每一个数都有选择在左或是在右两种选择,所以是2^(n-1),总共就是2^n,而这里包括了前两种的方案,所以要-4,最终应有2^n-2种。
看数据范围就知道要用快速幂,不过可惜如果只用快速幂会错,应为n和p的范围都是10^18,快速幂里在还没有模p之前的乘法都有可能超出long long int。所以想到用加法,a*b就是b个a相加,每一步都模p,然后用快速幂的原理不断二分,这就是所谓的快速乘法。
还有就是特殊数据,当n输入为1的时候情况数不是0而是1,当然不像BC上说的那样结果就是1,而是1MODp。
代码:
<span style="font-size:12px;color:#000000;">#include <iostream>#include<stdio.h>using namespace std;long long int n,p,res;long long modmul(long long a,long long b,long long m){ long long d,t; d=0; t=a; while(b>0) { if(b%2==1) d=(d+t)%m; b=b/2; t=(t+t)%m; } return d;}long long int modexp(long long a,long long b,long long m){ long long d,t; d=1; t=a; while (b>0) { if (b%2==1) d=modmul(d,t,m)%m; b/=2; t=modmul(t,t,m)%m; } return d;}int main(){ while(scanf("%I64d%I64d",&n,&p)!=EOF) { if(n==1) { printf("%I64d\n",n%p); continue; } res=(modexp(2,n,p)+p-2)%p; printf("%I64d\n",res); } //cout << "Hello world!" << endl; return 0;}</span>
0 0
- hdu5187_zhx's contest(快速幂+快速乘法)
- hdoj5187zhx's contest【快速幂+快速乘法】
- zhx's contest (hdu 5187 快速幂+快速乘法)
- HDU 5187 zhx's contest(快速幂+快速乘法)
- zhx's contest(快速幂+快速乘法)
- HDU 5187zhx's contest(快速幂乘法)
- hdu 5187 zhx's contest(快速幂,快速乘法,排列组合)
- HDU 5187 zhx's contest(快速乘法)
- HDU 5187-zhx's contest(快速乘法+快速幂取模)
- zhx's contest BestCoder Round #33(快速乘法+快速幂的原理)
- HDU - 5187 - zhx's contest (快速幂+快速乘)
- 数学(快速幂+快速加法 hdu5187-zhx's contest)
- hdu5187 zhx's contest题解(快速幂快速积)
- zhx's contest----快速幂+快速乘
- BestCoder Round #33(zhx's contest-快速乘法)
- hdu 5187 zhx and contest(快速幂、快速乘法)
- [练习][HDU5187]快速幂加强(快速幂+快速乘) zhx's contest
- 快速幂&&快速乘法
- 使用校园网进行ubuntu更新
- 呈现树与DOM树的关系
- UBUNTU、CENTOS搭建IPSEC/IKEV2 VPN服务器全攻略----Strongswan
- HDU 4443 Lost 树形概率DP
- HDU ACM 1279 验证角谷猜想
- hdu5187_zhx's contest(快速幂+快速乘法)
- PageRank算法简介及Map-Reduce实现
- Javascript Math ceil()、floor()、round()三个函数的区别
- Left:Firefox 36 VS Right:Chrome 41
- PHP中CURL详细教程
- 02 窗口与视图的基本概念和用法
- Win7与CentOS双系统时间差8个小时问题
- 黑马程序员——java基础——正则表达式
- Python中 == 与 is 的区别和应用场景