http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605

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

提示

 

来源

2013年山东省第四届ACM大学生程序设计竞赛
 
 
分析:
 

中等难度数论题。

先考虑 X = f(x)

可加可不加的欧拉定理优化 { 注意本题A,P并不一定互质,使用欧拉定理优化的时候注意细节。 A^(X + phi(P)) mod P = A^X mod P 在X>0时成立,注意X=0时和X为phi(P)的倍数时的特判 } 对于取模后的X,在P为质数时,数量级仍达到10^9,考虑到数据量n<=10^6且T=40,显然本题不可用O(logX)的快速幂求解,(希望确实做到了这点,如果有快速幂过的,如果能交流下优化的方法,感激不尽)。

考虑O(1)做法。 加欧拉定理优化可令ph = phi(P),也可以ph取10^9 + 1 我们可以找到一个最小的数 k 满足k*k>ph.

然后对于每一个X,令 X = i*k + j 其中i和j满足 0 <= i < k 且 0 <= j < k

则A^X = A^(i*k +j) = ( (A^k) ^ i ) * (A ^ j)。 A^k为定值,而i,j均<k,两部分%P的值都可以在O(k)时间内预处理出来 然后对于每个X进行O(1)求解。

接着考虑f(x),直接按照题意递推计算即可,需要注意的是x=1时,K没有对m取模,换言之,可能大于m

每组的复杂度为O(sqrt(P) + n)

 

官方代码:

 #include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
ll A,K,a,b,m,P;
ll powmod(ll a,ll n,ll m){
ll y=;
while(n){
if(n&) y=y*a%m;
n>>=;
a=a*a%m;
}
return y;
}
ll solve(){
ll f=K,sum=,tmp;
for(int i=;i<n;i++){
sum=(sum + powmod(A,f,P))%P;
f=(a*f + b)%m;
}
return sum;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in","r",stdin);freopen("out.txt","w",stdout);
#endif
int T,C=;
scanf("%d",&T);
while(T--)
{
scanf("%d%I64d%I64d%I64d%I64d%I64d%I64d",&n,&A,&K,&a,&b,&m,&P);
printf("Case #%d: %I64d\n",++C,solve());
}
return ;
}
 

sdutoj 2605 A^X mod P的更多相关文章

  1. sdut 2605 A^X mod P

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605 这个题卡的是优化,直观解法是在求x^y时 ...

  2. sdutoj 2624 Contest Print Server

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2624 Contest Print Server ...

  3. sdutoj 2608 Alice and Bob

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...

  4. sdutoj 2607 Mountain Subsequences

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607 Mountain Subsequence ...

  5. 函数mod(a,m)

    Matlab中的函数mod(a,m)的作用: 取余数 例如: mod(25,5)=0; mod(25,10)=5; 仅此.

  6. ORACLE 数据库 MOD 函数用法

    1.求2和1的余数. Select mod(2,1) from dual: 2能被1整除所以余数为0. 2.MOD(x,y)返回X除以Y的余数.如果Y是0,则返回X的值. Select mod(2,0 ...

  7. 黑科技项目:英雄无敌III Mod <<Fallen Angel>>介绍

    英雄无敌三简介(Heroes of Might and Magic III) 英3是1999年由New World Computing在Windows平台上开发的回合制策略魔幻游戏,其出版商是3DO. ...

  8. [日常训练]mod

    Description 给定$p_1,p_2,-,p_n,b_1,b_2,...,b_m$, 求满足$x\;mod\;p_1\;\equiv\;a_1,x\;mod\;p_2\;\equiv\;a_2 ...

  9. Apache Mod/Filter Development

    catalog . 引言 . windows下开发apache模块 . mod进阶: 接收客户端数据的 echo 模块 . mod进阶: 可配置的 echo 模块 . mod进阶: 过滤器 0. 引言 ...

随机推荐

  1. BZOJ3835: [Poi2014]Supercomputer

    Description Byteasar has designed a supercomputer of novel architecture. It may comprise of many (id ...

  2. C语言(4)

    C语言(4)--数据类型 C语言在用“/”是,注意左右两边都是整数时,商也是只有整数部分. 下面介绍一下C语言常用的数据类型:  注意: 1.char类型数据范围:256中字符. 2.float和do ...

  3. ArcGIS 设置地图显示范围大小(全屏显示)

    Arcmap的FullExtent默认是地图加载的时候的extent.其实这个fullExtent是可以设置的. 打开ArcMap,选择左边图例的Layers ,右键点击,选择“Properties. ...

  4. golang 简易聊天

    client.go ------------------------------ package main import ( "net" "fmt" " ...

  5. C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)

    #region//开机自动运行        private void CB_Auto_CheckedChanged(object sender, EventArgs e)        {//CB_ ...

  6. hbase的查询scan功能注意点(setStartRow, setStopRow)

    来自http://hi.baidu.com/7636553/blog/item/982beb17713bc004972b43ee.html hbase的scan查询功能注意项: Scan scan = ...

  7. JQuery插件让图片旋转任意角度且代码极其简单

    引入下方的jquery.rotate.js文件,然后通过$("选择器").rotate(角度);可以旋转任意角度, 例如$("#rotate-image").r ...

  8. 原生javascript封装ajax和jsonp

    在我们请求数据时,完成页面跨域,利用原生JS封装的ajax和jsonp: <!DOCTYPE html> <html lang="en"> <head ...

  9. [LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点

    Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 a ...

  10. Rocky4.2下安装金仓v7数据库(KingbaseES)

    1.准备操作系统 1.1 系统登录界面 1.2 操作系统版本信息 jdbh:~ # uname -ra Linux jdbh -x86_64 # SMP Fri Dec :: CST x86_64 G ...