山东第四届省赛C题: A^X mod P
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3232
Problem C:A^X mod P
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 10 Solved: 2
[Submit][Status][Discuss]
Description
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.
Input
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
Output
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.
Sample Input
2
3 2 1 1 1 100 100
3 15 123 2 3 1000 107
Sample Output
Case #1: 14
Case #2: 63
HINT
Source
和昨天做的福大的很像,但不是。
题目意思很简单,比赛用了枚举的方法,知道会超时,40组测试数据。
时间复杂度 o(n)=10^6*40*logn ==>10^9 每计算一个a^p%m logn
怎么做?
( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P.
看来别人的思路,才想到拆分。有了这个思想,就容易起来了,就是哈希。保存结果,每一次遇到,直接读取。避免了重复计算。
A^n=A^(k*x+y);
n=k*x+y;==>(A^k)^x * A^y;
A^k和A^y可以用一个dp1[ ]数组保存起来。这样就解决一个了。
(A^k)^x也用一个数组 dp2[ ]保存起来。
式子就可以转化为 A^n= dp2[n/k]*dp1[n%k]; 对比一下 (A^k)^x * A^y;
K取多大呢???10^6? 太大了,超了,没有必要这么大。
因为:
f(x) = (a*f(x-1) + b)%m , x > 1
f(x)最大是10^9 所以K=33333就足够了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define HH 33000
#define GG 33000
using namespace std; long long dp1[];
long long dp2[]; void make_ini(long long A,long long P)
{
long long i;
dp1[]=dp2[]=;
dp1[]=A;
for(i=;i<=HH;i++)
dp1[i]=(dp1[i-]*A)%P;//A^k dp2[]=dp1[HH];
for(i=;i<=GG;i++)
dp2[i]=(dp1[HH]*dp2[i-])%P;
} long long make_then(long long K,long long n,long long A,long long a,long long b,long long m,long long P)
{
long long ans=,i,j,tmp;
tmp=K;
for(i=;i<=n;i++)
{
ans=(ans+dp2[tmp/HH]*dp1[tmp%HH])%P;
tmp=(a*tmp+b)%m;
}
return ans;
} int main()
{
long long T,n,A,K,a,b,m,P,i,tmp;
while(scanf("%lld",&T)>)
{
for(i=;i<=T;i++)
{
scanf("%lld%lld%lld%lld%lld%lld%lld",&n,&A,&K,&a,&b,&m,&P);
make_ini(A,P);
tmp=make_then(K,n,A,a,b,m,P);
printf("Case #%lld: %lld\n",i,tmp);
}
}
return ;
}
山东第四届省赛C题: A^X mod P的更多相关文章
- 山东第四届省赛: Boring Counting 线段树
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- 第八届山东ACM省赛F题-quadratic equation
这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...
- 2019山东ACM省赛L题题解(FLOYD传递闭包的变形)
本题地址 https://cn.vjudge.net/contest/302014#problem/L Median Time Limit: 1 Second Memory Limit: 6 ...
- 振兴中华(蓝桥杯13年第四届省赛真题 JAVA-B组)
思路:因为只能横向或纵向跳到相邻的格子里,所以到'华'字有两种方法:①从左边的中横向跳过来 ②从上边的中纵向跳过来 直接递推即可. 标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格 ...
- 2013杭州现场赛B题-Rabbit Kingdom
杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...
- 2017年第六届数学中国数学建模国际赛(小美赛)C题解题思路
这篇文章主要是介绍下C题的解题思路,首先我们对这道C题进行一个整体的概括,结构如下: C题:经济类 第一问:发现危险人群. 发现:欺诈的方式开始.雇佣或浪漫的承诺. 数据→确定特定的经济萧条地区→确定 ...
- 2013年山东省赛F题 Mountain Subsequences
2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...
- 2013年省赛H题
2013年省赛H题你不能每次都快速幂算A^x,优化就是预处理,把10^9预处理成10^5和10^4.想法真的是非常巧妙啊N=100000构造两个数组,f1[N],间隔为Af2[1e4]间隔为A^N,中 ...
- 2013年省赛I题 Thrall’s Dream
2013年省赛I题判断单向联通,用bfs剪枝:从小到大跑,如果遇到之前跑过的点(也就是编号小于当前点的点),就o(n)传递关系. bfs #include<iostream> #inclu ...
随机推荐
- 975. Odd Even Jump
You are given an integer array A. From some starting index, you can make a series of jumps. The (1 ...
- Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mysql.plugin' doesn't exist
问题:在linux上安装mysql的时候出现Plugin ‘FEDERATED’ is disabled. /usr/sbin/mysqld: Table ‘mysql.plugin’ doesn’t ...
- Ceres入门笔记
介绍 Ceres可以解决下列形式的边界约束鲁棒非线性最小二乘问题 (1) $\min\limits_{x}\quad \frac{1}{2} \sum\limits_{i}\rho_{i}\left( ...
- javascript 模块依赖管理的本质
模块模式定义 模块是'javascript'的一种设计模式,它为函数定义一个包装函数,并且该包装函数的返回值与模块的API保持一致: function createModule() { functio ...
- ifconfig-dropped
drop的包是因为网卡的buffer满了 查看当前网卡的buffer size情况 ethtool -g eth0 Ring parameters for eth0: Pre-set maximums ...
- 【转】JMeter学习参数化User Defined Variables与User Parameters
偶然发现JMeter中有两个元件(User Defined Variables与User Parameters)很相近,刚开始时我也没注意,两者有什么不同.使用时却发现两者使用场景有些不同,现在小结一 ...
- MySQL实例crash的案例分析
[作者] 王栋:携程技术保障中心数据库专家,对数据库疑难问题的排查和数据库自动化智能化运维工具的开发有强烈的兴趣. [问题描述] 我们生产环境有一组集群的多台MySQL服务器(MySQL 5.6.21 ...
- Xcode10 libstdc++.6.0.9.tbd移除引起的错误
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/u ...
- ActiveRecord::Fixture::FixtureError: table "users" has no column named "activated_at".
window 7+ruby2.33+rails5.0. 在测试的时候 rails test 报固件fixture错误: 没有某列字段存在 虽然可以直接通过开发框架去修改字段,但是开发过程中应该通过迁移 ...
- 堆排序详解以及java实现
前言 临近毕业,开始找工作,近期一直在看算法导论(CLRS)同时各种刷题.希望以后有时间把所有学习心得和刷题心得记录下来. 堆 堆排序和合并排序一样,是一种时间复杂度为O(nlgn)的算法,同时和插入 ...