proportional sequence

I read this senior's blog, so I want to record how to quickly find the sum of the proportional sequence involved;

Link: https://blog.csdn.net/keepcoral/article/details/80144750

Suppose we now know a1, q, n, let us find the sum of the proportional sequence. If this sum is very large, we need to take the modulo, so we also know the mod.

That is, a1, q, n, mod are known.

To add math knowledge, sometimes I forget:

an=a1*q^(n-1), the sum of the proportional sequence Sn= a1+a1*q+a1*q^2+...+a1*q^(n-1) = a1*(1+ q+q^2+...+q^(n-1)) =a1*(1-q^n)/(1-q);

                Here are mainly the two bold formulas above

When summing, we first put a1 out, let's calculate (1+q+q^2+...+q^(n-1));

ll T(ll q,ll n)
{
    if(n==1)
    return 1;
    ll date=T(q,n/2);//step1
    date=(date+date*quick(q,n/2))%mod;//step2
    if(n%2)
    date=(date+quick(q,n-1))%mod;//step3
    return date;
}

I'll add another example here: if n is 15; we push back when we recurse to n==1;

The recursive order is n=15, n=7, n=3, n=1;

We ask for 1+q+q^2+q^3+...+q^14;

we push back

n==3,step1:date=1        step2:date=1+q          step3date=1+q+q^2;

n==7,  step1:date=1+q+q^2       step2:date=1+q+q^2+q^3+q^4+q^5   step3:date=1+q+q^2+q^3+q^4+q^5+q^6

Too long, be lazy

n==15,step1:date=1+...+q^6      step2:date=1+...+q^6+...+q^13   step3:date=1+...+q^14;

Code:

#include<stdio.h>
#include<string.h>
#define mod 10000007
#define ll long long
ll n,m,k,t;
ll quick(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        years =a*years% mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
ll T(ll q,ll n)
{
    if(n==1)
    return 1;
    ll date=T(q,n/2);
    date=(date+date*quick(q,n/2))%mod;
    if(n%2)
    date=(date+quick(q,n-1))%mod;
    return date;
}
intmain ()
{
    ll q,n,a1;
    scanf("%lld%lld%lld",&a1,&q,&n);
    ll ans=T(q,n);
    years = years*a1% mod;
    printf("%lld",ans);
    return 0;
 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325087027&siteId=291194637