[原]sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)
本文出自:http://blog.csdn.net/svitter
题意:
f(x) = K, x = 1
f(x) = (a*f(x-1) + b)%m , x > 1
求出( 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
本题目的关键在于大幂的分解和。。你要这样想,因为不停的求A的幂,所以肯定把算过的保存下来最合适。
打表。(- -)每次都是打表,shit。
把f分解为 fix * k + j 的形式,f就是f(x)的简称。(哈希)
然后关键是fix怎么整,多少合适。我觉得33333合适。为啥?
因为10^9 / 33333 =30000数组不大。然后余数也在33333之内,其好,那就它吧。
然后就用普通的幂模相乘打表就可以f[i] = (f[i-1] * a)mod p,这是个简单的例子,a和p没有实际含义。
这个题目我在做的时候蛋疼到二分法动态打表,但是超空间,因为不停的递归调用造成了超空间。。但是我觉得如果能够实现。。用栈的方法,应该会更加节省时间。因为用到的才计算。如果有不同的意见,请回复我,谢谢。
AC代码:
//============================================================================
// Name : math.cpp
// Author : Vit
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath> using namespace std;
#define lln long long int
#define fix 33333 //num
lln n, A, K, a, b, m, P; //A^f f = fix * k + j
// f = dp1 * k + dp2 lln dpk[30001];
lln dpj[33334]; void init()
{
int i, j; //init hash
dpj[1] = A;
dpj[0] = dpk[0] = 1; for(i = 2; i <= 33333; i++)
dpj[i] = (dpj[i-1] * A) % P; dpk[1] = dpj[33333];
for(j = 1; j <= 30000; j ++)
dpk[j] = (dpk[j-1] * dpk[1]) % P;
} void ace()
{
//work pit;
int i, t, c;
lln ans, f;
scanf("%d", &t);
for(c = 1; c <= t; c++)
{
scanf("%lld%lld%lld%lld%lld%lld%lld",&n, &A, &K, &a, &b, &m, &P);
//init
init();
f = K;
ans = 0;
for(i = 0; i < n; i++)
{
ans = (ans + dpk[f/fix] * dpj[f % fix]) % P;
f = (a * f + b) % m;
}
printf("Case #%d: %lld\n", c, ans);
}
} int main()
{
ace();
return 0;
}
[原]sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)的更多相关文章
- [原]sdut2624 Contest Print Server (大水+大坑)山东省第四届ACM省赛
本文出自:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&am ...
- 山东省第四届ACM省赛
排名:http://acm.sdut.edu.cn/sd2012/2013.htm 解题报告:http://www.tuicool.com/articles/FnEZJb A.Rescue The P ...
- 2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server
点击打开链接 2226: Contest Print Server Time Limit: 1 Sec Memory Limit: 128 MB Submit: 53 Solved: 18 [Su ...
- 山东省第四届ACM大学生程序设计竞赛解题报告(部分)
2013年"浪潮杯"山东省第四届ACM大学生程序设计竞赛排名:http://acm.upc.edu.cn/ranklist/ 一.第J题坑爹大水题,模拟一下就行了 J:Contes ...
- sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛
Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
- 山东省第四届acm解题报告(部分)
Rescue The PrincessCrawling in process... Crawling failed Description Several days ago, a beast ca ...
- 山东省第四届ACM程序设计竞赛部分题解
A : Rescue The Princess 题意: 给你平面上的两个点A,B,求点C使得A,B,C逆时针成等边三角形. 思路: http://www.cnblogs.com/E-star/arch ...
- Sdut 2409 The Best Seat in ACM Contest(山东省第三届ACM省赛 H 题)(模拟)
题目描述 Cainiao is a university student who loves ACM contest very much. It is a festival for him once ...
随机推荐
- MySQL 绿色版安装方法图文教程
一.下载,这里使用绿色解压缩版 http://mirror.services.wisc.edu/mysql/Downloads/MySQL-5.1/mysql-noinstall-5.1.32-win ...
- Python标准库09 当前进程信息 (os包)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们在Linux的概念与体系,多次提及进程的重要性.Python的os包中有查询和 ...
- python输出重定向
0表示标准输入1表示标准输出2表示标准错误输出> 默认为标准输出重定向,与 1> 相同2>&1 意思是把 标准错误输出 重定向到 标准输出.&>file 意思是 ...
- android 插件化 模块化开发
http://blog.csdn.net/o1587790525/article/details/11891997 Android 插件化架构设计 http://www.iqiyi.com/w_19 ...
- dede数据库类使用方法$dsql【转】
http://www.cnblogs.com/xcxc/p/3601909.html dedecms的数据库操作类,非常实用,在二次开发中尤其重要,这个数据库操作类说明算是奉献给大家的小礼物了. 引入 ...
- [Java] 02 String的常用方法
public class TestString{ public static void main(String[] args){ String str1 = "123"; Stri ...
- [Uva 10085] The most distant state (BFS)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- isIsomorphic
超时版: /* Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if t ...
- adb bugreport > d:/bug.txt
adb bugreport > d: 快速下载anr等bug日志
- 安装Ubuntu下的开发工具
刚安装好的Ubuntu,还缺乏很多开发工具.这些工具都可以通过网络进行安装. 1. 更新软件源$ sudo apt-get update 2.安装.配置.启动ftp服务.执行以下命令安装,安装后即会自 ...