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. 引言 ...
随机推荐
- thinkphp常用Config.php配置项
<?php return array( //'配置项'=>'配置值' 'DB_HOST' => 'localhost', 'DB_USER' => 'root', 'DB_PW ...
- js_实用
[js提示] 文件中加 /// <reference path="exp_validate.js" /> 即可在当前js文件使用exp_validate.js中全局变量 ...
- MVVM Command Binding: InvokeCommandAction v.s. EventToCommand
This gives you the ability to create a trigger on an event and bind it to an ICommand on the view mo ...
- Windows Phone 8 MVVM
Why? 1 reason MVVM works really well with XAML based applications is because of the powerful XAML bi ...
- Error #include nested too deeply
转载:http://blog.csdn.net/ysdaniel/article/details/7043395 出现 Error #include nested too deeply 原因是: 头文 ...
- android- 菜单
选项菜单:menu_main.xml <?xml version="1.0" encoding="utf-8"?><menu xmlns:an ...
- AFN----AFNetworking
一.介绍 官方介绍: 1.适用于iOS和Mac OS X两个平台的网络库 2.基于Foundation URL Loading System上进行一套封装 3.提供了丰富的API接口 4.是一个轻量级 ...
- java中hashCode方法与equals方法的用法总结
首先,想要明白hashCode的作用,必须要先知道Java中的集合. 总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set. 前者集合内的元素是有序的,元素可以重 ...
- WPF 打开文件、文件夹
打开文件代码: OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = &quo ...
- php中htmlspecialchars和htmlentiti
Certain characters have special significance in HTML, and should be represented by HTML entities if ...