数列 [Bzoj3142,Codevs2089,HNOI2013]
2016-06-02 16:31
351 查看
题目地址——
股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为 N。
在疯涨的 K 天中 小T 观察到:
除第一天外每天的股价都比前一天高,且高出的价格(即当天的股价与前一天的股价之差)不会超过 M,M 正整数。
并且这些参数满足 M(K−1)<N。
小T忘记了这K天每天的具体股价了,他现在想知道这 K 天的股价有多少种可能。
对 P 的说明参见后面“输出格式”中对 P 的解释。
输入保证 20% 的数据 M,N,K,P≤20000,保证 100% 的数据 M,K,P≤109,N≤1018。
{1,2,3},{1,2,4},{1,3,4},{1,3,5},
{2,3,4},{2,3,5},{2,4,5},{2,4,6},
{3,4,5},{3,4,6},{3,5,6},{3,5,7},
{4,5,6},{4,5,7},{4,6,7},{5,6,7}。
用快速幂就可以 A 了。
#include <cstdio>
#define LL long long
#define Min(x,y) ((x)<(y)?(x):(y))
using namespace std;
LL n,k,m,p;
LL tp;
inline LL in(){
LL ans=0;
char x=getchar();
while(x<‘0’||x>‘9’)x=getchar();
while(x>=‘0’&&x<=‘9’){ans=ans*10+x-‘0’;x=getchar();}
return ans;
}
LL power(LL x,LL y){
if(y==0)return 1;
if(y==1)return x%p;
LL tmp=power(x,y/2);
if(y&1)return tmp*tmp%p*(x%p)%p;
else return tmp*tmp%p;
}
LL power2(LL x,LL y){
if(y==0)return 1;
if(y==1)return x%tp;
LL tmp=power2(x,y/2);
if(y&1)return tmp*tmp%tp*(x%tp)%tp;
else return tmp*tmp%tp;
}
int main(){
n=in();k=in();m=in();p=in();
tp=2*p;
LL t1=power(m,k-1)*(n%p)%p;
LL t2=(k-1)%p;
LL tmp=power2(m,k-1);
LL t3=(tmp+tmp*m)%tp/2;
LL ans=((t1-t2*t3%p)%p+p)%p;
printf(”%lld\n”,ans);
return 0;
}
数列
【问题描述】
小T 最近在学着买股票,他得到内部消息:F公司 的股票将会疯涨。股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为 N。
在疯涨的 K 天中 小T 观察到:
除第一天外每天的股价都比前一天高,且高出的价格(即当天的股价与前一天的股价之差)不会超过 M,M 正整数。
并且这些参数满足 M(K−1)<N。
小T忘记了这K天每天的具体股价了,他现在想知道这 K 天的股价有多少种可能。
【输入描述】
输入文件只有一行用空格隔开的四个数:N、K、M、P 。对 P 的说明参见后面“输出格式”中对 P 的解释。
输入保证 20% 的数据 M,N,K,P≤20000,保证 100% 的数据 M,K,P≤109,N≤1018。
【输出描述】
仅包含一个数,表示这 K 天的股价的可能种数对于 P 的模值。【输入样例】
7 3 2 997【输出样例】
16【数据范围及提示 Data Size & Hint】
输出样例的16表示输入样例的股价有16种可能:{1,2,3},{1,2,4},{1,3,4},{1,3,5},
{2,3,4},{2,3,5},{2,4,5},{2,4,6},
{3,4,5},{3,4,6},{3,5,6},{3,5,7},
{4,5,6},{4,5,7},{4,6,7},{5,6,7}。
【Solution】
ans=mk−1n−(k−2)(mk+1+mk2) mod p用快速幂就可以 A 了。
【Code】
[cpp] #include <iostream>#include <cstdio>
#define LL long long
#define Min(x,y) ((x)<(y)?(x):(y))
using namespace std;
LL n,k,m,p;
LL tp;
inline LL in(){
LL ans=0;
char x=getchar();
while(x<‘0’||x>‘9’)x=getchar();
while(x>=‘0’&&x<=‘9’){ans=ans*10+x-‘0’;x=getchar();}
return ans;
}
LL power(LL x,LL y){
if(y==0)return 1;
if(y==1)return x%p;
LL tmp=power(x,y/2);
if(y&1)return tmp*tmp%p*(x%p)%p;
else return tmp*tmp%p;
}
LL power2(LL x,LL y){
if(y==0)return 1;
if(y==1)return x%tp;
LL tmp=power2(x,y/2);
if(y&1)return tmp*tmp%tp*(x%tp)%tp;
else return tmp*tmp%tp;
}
int main(){
n=in();k=in();m=in();p=in();
tp=2*p;
LL t1=power(m,k-1)*(n%p)%p;
LL t2=(k-1)%p;
LL tmp=power2(m,k-1);
LL t3=(tmp+tmp*m)%tp/2;
LL ans=((t1-t2*t3%p)%p+p)%p;
printf(”%lld\n”,ans);
return 0;
}
相关文章推荐
- 【android】:android实现监听多个按钮事件
- 我的 VC++ 常见错误问题解决办法
- JavaScript:闭包
- PHP 5.5 新特性关键字 yield
- Python变量与常量
- shell 实例收集
- Picasso入门教程(三)
- CodeForces 24B-F1 Champions
- HDMI定义说明
- Codeforces #355 div.2
- 中广核项目进展
- Kubernetes开山篇
- mac下安装java开发环境===so easy.
- 浅谈编译高通android5.1源代码
- TextSwitcher类(TextView改变文本动画切换)
- Android.mk官方说明 中文翻译
- block
- 事件分发机制--解决事件冲突的经典方法
- BZOJ 2626 JZPFAR(KD-tree)
- Spring MVC之@RequestBody, @ResponseBody 详解