sdutoj 2605 A^X mod P
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.
输入
1 <= n <= 10^6
0 <= A, K, a, b <= 10^9
1 <= m, P <= 10^9
输出
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
提示
来源
中等难度数论题。
先考虑 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的更多相关文章
- sdut 2605 A^X mod P
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605 这个题卡的是优化,直观解法是在求x^y时 ...
- sdutoj 2624 Contest Print Server
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2624 Contest Print Server ...
- sdutoj 2608 Alice and Bob
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...
- sdutoj 2607 Mountain Subsequences
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607 Mountain Subsequence ...
- 函数mod(a,m)
Matlab中的函数mod(a,m)的作用: 取余数 例如: mod(25,5)=0; mod(25,10)=5; 仅此.
- 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 ...
- 黑科技项目:英雄无敌III Mod <<Fallen Angel>>介绍
英雄无敌三简介(Heroes of Might and Magic III) 英3是1999年由New World Computing在Windows平台上开发的回合制策略魔幻游戏,其出版商是3DO. ...
- [日常训练]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 ...
- Apache Mod/Filter Development
catalog . 引言 . windows下开发apache模块 . mod进阶: 接收客户端数据的 echo 模块 . mod进阶: 可配置的 echo 模块 . mod进阶: 过滤器 0. 引言 ...
随机推荐
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- 优化UITableViewCell高度计算的那些事
优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...
- Jquery_AjaxFileUpload插件的使用记录
功能需求:Spring MVC框架下,实现无刷新页面上传图片,并展示图片预览效果 直接上代码: 1.图片预览效果 <%@ page contentType="text/html;cha ...
- JS调用水晶报表打印翻页按钮事件
默认的水晶报表打印按钮.翻页按钮太小,并且样式不好调整,考虑自己做一个按钮,然后调用水晶报表的按钮事件. 在实际操作中发现可以在.net按钮的服务器端事件中调用翻页方法: CrystalReportV ...
- (转)FTP操作类,从FTP下载文件
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- Perform UPSERT / INSERT OR UPDATE against a SQLite Database
Option 1: You can afford deleting the row In other words, you don't have foreign key, or if you have ...
- Point Grey FlyCapture 实例汇总
Example Language Description AsyncTriggerEx C++ Demonstrates some of the basic asynchronous trigger ...
- R中list对象属性以及具有list性质的对象
R语言list的特点:It has length, [[ and [ methods, and is recursive because list can contain other list!上图显 ...
- print_r、echo、var_dump三者的区别
这三者均是php里输出的方式. 1.print_r用于输出数组. 2.echo只能用于输出字符串,在没有框架的时候,多用这种方法将结果打印在html页面上. 3.var_dump能输出任何类型的数据, ...
- Spring的quartz定时器同一时刻重复执行二次的问题解决
最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...