sdut2605——A^X mod P

来源:互联网 发布:上古卷轴5ece捏脸数据 编辑:程序博客网 时间:2024/06/05 14:06

A^X mod P

Time Limit: 5000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

It's easy for ACMer to calculate A^X mod P. Now given seven integers n, A, K, a, b, m, P, and a function f(x) which defined as following.

f(x) = K, x = 1

f(x) = (a*f(x-1) + b)%m , x > 1


Now, Your task is to calculate

( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P. 

输入

 In the first line there is an integer T (1 < T <= 40), which indicates the number of test cases, and then T test cases follow. A test case contains seven integers n, A, K, a, b, m, P in one line.

1 <= n <= 10^6

0 <= A, K, a, b <= 10^9

1 <= m, P <= 10^9

输出

 For each case, the output format is “Case #c: ans”. 

c is the case number start from 1.

ans is the answer of this problem.

示例输入

2
3 2 1 1 1 100 100
3 15 123 2 3 1000 107

示例输出

Case #1: 14
Case #2: 63
题意:按题意求和,非打表不行,用不到快速幂,,,
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <algorithm>#include <vector>#include <map>#include <string>#include <stack>using namespace std;typedef long long ll;#define PI 3.1415926535897932#define E 2.718281828459045#define INF 0xffffffff//0x3f3f3f3f#define mod 997
const int M=1005;int n,m;int cnt;int sx,sy,sz;int mp[1000][1000];int pa[M*10],rankk[M];int head[M*6],vis[M*100];int dis[M*100];ll prime[M*1000];bool isprime[M*1000];int lowcost[M],closet[M];char st1[5050],st2[5050];int len[M*6];typedef pair<int ,int> ac;//vector<int> g[M*10];ll dp[50][20][2];int has[10500];int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};void getpri(){    ll i;    int j;    cnt=0;    memset(isprime,false,sizeof(isprime));    for(i=2; i<1000000LL; i++)    {        if(!isprime[i])prime[cnt++]=i;        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)        {            isprime[i*prime[j]]=1;            if(i%prime[j]==0)break;        }    }}struct node{    int v,w;    node(int vv,int ww)    {        v=vv;        w=ww;    }};vector<int> g[M*100];char str[100005];ll di[M*M*10];ll f[M*M*10];int bit[50];int A,K,a,b,P;void init(){    //先把A的1e9分段存储,用到时直接调用    di[0]=1;    for(int i=1;i<=33333;i++)//33333的平方够覆盖1e9        di[i]=(di[i-1]*A)%P;    ll tmp=di[33333];    f[0]=1;    for(int i=1;i<=33333;i++){        f[i]=(tmp*f[i-1])%P;    }}ll qmod(int x,int p){    ll ans=1;    while(p){        if(p&1)            ans=(ans*x)%P;        x=(x*x)%P;        p>>=1;    }    return ans%P;}ll solution(int n){    ll fi=K;  //这里必须开ll  int最大不到1e9    ll ans=0;    for(int i=1;i<=n;i++){        ans=(ans+(di[fi%33333]*f[fi/33333])%P)%P;//fi[]是结果的主干((A^(33333))^f[]),di[]是剩余的项(A^fi)        fi=(a*fi+b)%m;    }    return ans%P;}int main(){    int i,j,k,t;    bool flag;    //memset(dp,-1,sizeof(dp));    scanf("%d",&t);    for(i=1; i<=t; i++)    {        scanf("%d%d%d%d%d%d%d",&n,&A,&K,&a,&b,&m,&P);        init();        printf("Case #%d: %d\n",i,solution(n));    }    return 0;}
0 0
原创粉丝点击