2019ACM河北省省赛——Icebound and Sequence

题目描述
Icebound hates math. But Imp loves math. One day, Imp gave icebound a problem.

The problem is as follows.在这里插入图片描述
For given q,n,p, you need to help icebound to calculate the value of S.
输入描述:
The first line contains an integer T, denoting the number of test cases.
The next T lines, each line contains three integers q,n,p, separated by spaces.
1≤T≤100,1≤n,q,p≤10 9
输出描述:
For each test case, you need to output a single line with the integer S.
示例1
输入

2
2 3 100
511 4 520

输出

14
184

思路
参考博客
这个ACM我是一道也不会。。。┭┮﹏┭┮
此题知识点
在这里插入图片描述
为啥要用这个公式呢,因为数太大了。。。
还有就是pow_m()求a的b次方的函数的方法需要记一下,别的方法运行超时
代码

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll pow_m(ll a,ll b,ll m){
	 ll res=1;
    while(b)
    {
        if(b%2)
            res=(res*a)%m;
        a=(a*a)%m;
        b/=2;
    }
    return res;
}
ll solve(ll q,ll n,ll p){
	if(n==1) return q%p;
	if(n%2==0) return (1+pow_m(q,n/2,p))*solve(q,n/2,p)%p;
	else return ((1+pow_m(q,n/2,p))*solve(q,n/2,p)%p+pow_m(q,n,p))%p;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		ll q,n,p;
		cin>>q>>n>>p;
		ll sum=solve(q,n,p);
		cout<<sum<<endl;
	} 
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值