B-Icebound and Sequence(等比数列求和取模)

题目传送门:B-Icebound and Sequence(19年河北省赛)

题目大意:

等比数列求和,结果取模

分析:

因为取模操作,直接运用等比数列求和公式无法做出,所以需要用到公式

求等比为k的等比数列之和S[n]..当n为偶数..S[n] = S[n/2] + pow(k,n/2) * S[n/2]

n为奇数...S[n] = S[n/2] + pow(k,n/2) * S[n/2] + pow(k,n)等比数列第n个数的值

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,q,p;
ll pow_m(ll a,ll b,ll m)
{
    ll res=1;
    while(b)
    {
        if(b&1)
            res=(res*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    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--)
    {
        cin>>q>>n>>p;
        cout<<solve(q,n,p)<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/LjwCarrot/p/10926000.html

时间: 2024-08-13 05:08:12

B-Icebound and Sequence(等比数列求和取模)的相关文章

hoj3152-Dice 等比数列求和取模

http://acm.hit.edu.cn/hoj/problem/view?id=3152 Dice My Tags (Edit) Source : Time limit : 1 sec Memory limit : 128 M Submitted : 82, Accepted : 18 You have a dice with M faces, each face contains a distinct number. Your task is to calculate the expect

等比数列二分求和取模

题意:Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. n (n ≤ 30), k (k ≤ 109) and m (m < 104) 输出结果矩阵 解法: 若 n是偶数 Sn= a+...+an/2 + an/2+1 + an/2+2 +...+ an/2+n/2 =(a+...+an/2) + an/2(a+...+an/2) =Sn/2+ an/2Sn/2 =(1+

[hdu5226]组合数求和取模(Lucas定理)

题意:给一个矩阵a,a[i][j] = C[i][j](i>=j) or 0(i < j),求(x1,y1),(x2,y2)这个子矩阵里面的所有数的和. 思路:首先问题可以转化为求(0,0),(n,m)这个子矩阵的所有数之和.画个图容易得到一个做法,对于n<=m,答案就是2^0+2^1+...+2^m=2^(m+1)-1,对于n>m,答案由两部分构成,一部分是2^(m+1)-1,另一部分是sigma i:m+1->n f[i][m],f[i][m]表示第i行前m列的数之和,f

Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to

HDU - 2817 - A sequence of numbers (快速幂取模!)

A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3494    Accepted Submission(s): 1073 Problem Description Xinlv wrote some sequences on the paper a long time ago, they migh

杭电 2817 A sequence of numbers【快速幂取模】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 解题思路:arithmetic or geometric sequences 是等差数列和等比数列的意思, 即令输入的第一个数为a(1),那么对于等差数列 a(k)=a(1)+(k-1)*d,即只需要求出 a(k)%mod   又因为考虑到k和a的范围, 所以对上式通过同余作一个变形:即求出 (a(1)%mod+(k-1)%mod*(d%mod))%mod 对于等比数列 a(k)=a(1)*q

Sumdiv 等比数列求和

Sumdiv Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15364   Accepted: 3790 Description Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of

[hdu 4959]Poor Akagi 数论(卢卡斯数,二次域运算,等比数列求和)

Poor Akagi Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 131    Accepted Submission(s): 29 Problem Description Akagi is not only good at basketball but also good at math. Recently, he got a

POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1845 Appoint description:  System Crawler  (2015-05-27) Description Consider two natural numbers A and B. Let S be the sum of all natural d