HDU 4794 Arnold (Fib数模 n 的应用)——2013 Asia Changsha Regional Contest

本文介绍了一种基于Arnold变换的图像处理方法,并探讨了如何通过数学运算确定该变换达到初始状态所需的最小步骤数。文章提供了详细的算法实现,包括如何处理大数运算及优化计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

Arnold

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 391    Accepted Submission(s): 100


Problem Description
Do you know Vladimir Arnold? He’s a mathematician who demonstrated an image transformation method called arnold transformation, which could shuffle all pixels in an image, and after a serials of this transformation, the image would be transformed to its original form.
The transformation method is quite simple. For a given image with N × N pixels (Width and height are both equal to N), a pixel at location (x, y) will be shuffled to location ( (x + y) % N , (x + 2 × y) % N ) (0 ≤ x < N, 0 ≤ y < N). In one step of transformation, all N × N pixels will be shuffled to new corresponding location, making the image a chaotic one. You can do the transformation as many times as you can.
The arnold transformation is very interesting. For every image of size N × N, after finite steps of transformation, the image will become exact the same as the original one. The minimum number of steps which make every possible image become the same as origin will be called as period of arnold transformation. For a given N, can you calculate the period?
 

Input
There will be about 200 test cases. For each test case, there will be an integer N in one line. Here N (2 ≤ N ≤ 4000000000) equals to width and height of images.
 

Output
For each test case, please calculate the period of arnold transformation and output it in one line.
 

Sample Input
  
11
29
41
 

Sample Output
  
5
7
20

题目大意:

给定一个 [Math Processing Error] 的矩阵,现在经过这样一个变换:将 [Math Processing Error] 变为 [Math Processing Error] 现在求经过多少次这样的变换之后在回到 [Math Processing Error] 的原始矩阵。

解题思路:

首先我们取一个特殊点比如说: [Math Processing Error], 我们发现这是一个斐波那契数列,那么现在就转化为求斐波那契数列在模 [Math Processing Error] 下的值,但是因为两个斐波那契数列才占了一个坐标,所以最后结果得除以 [Math Processing Error], 具体怎么计算斐波那契模 [Math Processing Error] 下的值呢,可以参考 Acdreamer大神的博客

这道题基本就是模板题目:

[Math Processing Error]

/**
2016 - 10 - 06 下午
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
///typedef long long LL;
///!!!!有时候 LL 装不下
typedef unsigned long long LL;
const int INF = 1e9+5;
const int MAXN = 2;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
typedef struct {
    LL mat[MAXN][MAXN];
}Matrix;
const Matrix I = {1, 0,
            0, 1,
            };

const Matrix P = {1, 1,
            1, 0,
            };

Matrix Matrix_Multi(Matrix a, Matrix b, LL MOD){
    Matrix c;
    for(int i=0; i<MAXN; i++){
        for(int j=0; j<MAXN; j++){
            c.mat[i][j] = 0;
            for(int k=0; k<MAXN; k++)
                c.mat[i][j] = (c.mat[i][j] + a.mat[i][k]*b.mat[k][j]%MOD)%MOD;///注意这里可能会很大的数
        }
    }
    return c;
}
Matrix Matrix_Pow_Mod(Matrix a, LL b, LL MOD){
    Matrix ans = I;
    while(b){
        if(b & 1)
            ans = Matrix_Multi(ans, a, MOD);
        b>>=1LL;
        a = Matrix_Multi(a, a, MOD);
    }
    return ans;
}
LL Pow_Mod(LL a, LL b, LL MOD){
    LL ans = 1LL;
    while(b){
        if(b & 1)
            ans = (ans * a) % MOD;
        b>>=1LL;
        a = (a * a) % MOD;
    }
    return ans;
}
LL GCD(LL a, LL b){
    if(b == 0)
        return a;
    return GCD(b, a % b);
}
const LL N = 1e5+5;
const LL NN = 1e3+5;
int prime[N];
LL p[N], k;
void isprime(){///素数筛
    memset(prime, 0, sizeof(prime));
    k = 0;
    for(LL i=2; i<N; i++){
        if(!prime[i]){
            p[k++] = i;
            for(LL j=i+i; j<N; j+=i)
                prime[j] = 1;
        }
    }
}
int legendre(LL a, LL p){
    if(Pow_Mod(a,(p-1)>>1LL,p) == 1) return 1;
    return 0;
}
LL cnt = 0, fac[NN], num[NN];
void Dec(LL n){///分解素因子
    cnt = 0;
    memset(num, 0, sizeof(num));
    for(LL i=0; p[i]*p[i]<=n&&i<k; i++){
        if(n % p[i] == 0){
            fac[cnt] = p[i];
            while(n % p[i] == 0){
                n /= p[i];
                num[cnt]++;
            }
            cnt++;
        }
    }
    if(n > 1){
        fac[cnt] = n;
        num[cnt++] = 1;
    }
}
LL yinzi[NN];///因子
LL cntc = 0;
void get_Num(LL n){///得到所有的因子
    cntc = 0;
    for(LL i=1; i*i<=n; i++){
        if(n % i == 0){
            if(i * i != n)
                yinzi[cntc++] = n / i;
            yinzi[cntc++] = i;
        }
    }
}
LL Find_Loop(LL n){///找循环节
    Dec(n);
    LL ans = 1;
    for(LL i=0; i<cnt; i++){
        LL record = 1;
        if(fac[i] == 2)
            record = 3;
        else if(fac[i] == 3)
            record = 8;
        else if(fac[i] == 5)
            record = 20;
        else{
            if(legendre(5, fac[i]))
                get_Num(fac[i]-1);
            else
                get_Num(2*(fac[i]+1LL));
            ///cout<<cntc<<endl;
            sort(yinzi, yinzi+cntc);
            ///cout<<cntc<<endl;
            for(LL k=0; k<cntc; k++){
                Matrix tmp = Matrix_Pow_Mod(P, yinzi[k]-1, fac[i]);
                LL x = (tmp.mat[0][0] + tmp.mat[0][1]) % fac[i];
                LL y = (tmp.mat[1][0] + tmp.mat[1][1]) % fac[i];
                if(x==1 && y==0){
                    record = yinzi[k];
                    break;
                }
            }
        }
        for(LL k=1; k<num[i]; k++)
            record *= fac[i];
        ans = (ans / GCD(ans, record))* record;
    }
    return ans;
}
int main()
{
    LL n;
    isprime();
    while(~scanf("%lld",&n)){
        if(n == 2LL){
            puts("3");
            continue;
        }
        printf("%lld\n",Find_Loop(n)>>1LL);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值