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省赛(打表,快速幂模思想,哈希)

时间: 2024-11-07 08:03:57

sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)的相关文章

sdut 2603 Rescue The Princess(算是解析几何吧)(山东省第四届ACM省赛A题)

题目地址:sdut 2603 Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess

sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛

Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees and flowers on the mountain, and there are many animals and birds also. Coco like the mountain so much that she now name some letter s

Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1). Then Alice as

[2013山东省第四届ACM大学生程序设计竞赛]——Alice and Bob

Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1). T

[2013山东省第四届ACM大学生程序设计竞赛]——Rescue The Princess

Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess set out immedia

Sdut2411 Pixel density 山东省第三届ACM省赛(输入输出字符串处理)

本文出处:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2411 题意:给你一个串,让你依据那个串来输出ppi.坑特别多.ppi的计算方法是dp / inches; dp = sqrt(wp*wp + hp * hp); 现在我来说说这个题目有多坑: 给你的串的格式是这样: name + inches+ "inches"

Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

Time Limit: 5000MS Memory limit: 65536K 题目描写叙述 Haveyou ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for NvidiaTegra 2 based Android devices) is a video game developed by Hal

山东省第四届acm.Rescue The Princess(数学推导)

Rescue The Princess Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 412  Solved: 168 [Submit][Status][Web Board] Description Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince wh

山东省第四届ACM程序设计竞赛A题:Rescue The Princess(数学+计算几何)

Rescue The Princess Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 412  Solved: 168[Submit][Status][Web Board] Description Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who