您的位置:首页 > 其它

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

2016-10-06 18:36 513 查看
传送门

Arnold

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


[align=left]Problem Description[/align] 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?
[align=left]Input[/align] 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.
[align=left]Output[/align] For each test case, please calculate the period of arnold transformation and output it in one line.
[align=left]Sample Input[/align]
11

29

41

[align=left]Sample Output[/align]
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
;
LL p
, 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: