UVALive - 3722 找规律

Posted The Azure Arbitrator

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVALive - 3722 找规律相关的知识,希望对你有一定的参考价值。

题意:找规律
题解:找规律
结论是\(a^n(x-1)-\sum_{i=1}^{n-1}a^i \mod\ c\)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar(‘\n‘)
#define blank putchar(‘ ‘)
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e6+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll MOD = 1e9+7;
inline ll mod(ll a){return a%MOD;}
ll read(){
    ll x=0,f=1;register char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
ll fpw(ll a,ll n){
    ll ans=1;
    while(n){
        if(n&1) ans=mod(ans*a);
        n>>=1;
        a=mod(a*a);
    }
    return ans;
}
int sum(int p,int n){
    if(n==0)return 1;
    if(n&1) return mod(mod(1+fpw(p,n+1>>1))*mod(sum(p,n-1>>1)));
    else return mod(mod(1+fpw(p,n>>1))*mod(sum(p,(n>>1)-1))+mod(fpw(p,n)));
}
int main(){
    ll x,a,n;
    while(cin>>x>>a>>n>>MOD){
        if(x==0&&a==0&&n==0&&MOD==0)break;
        cout<<mod(mod(mod(fpw(a,n)*(x-1))-sum(a,n-1)+MOD)+1+MOD)<<endl;
    }
    return 0;
}

以上是关于UVALive - 3722 找规律的主要内容,如果未能解决你的问题,请参考以下文章

UVaLive 6847 Zeroes (找规律,水题)

UVALive - 6442

PAT1049-----枚举法,找规律题,注意降低时间复杂度

uvalive 7500 Boxes and Balls

JavaScript笔试题(js高级代码片段)

UVALive - 6859 Points