HDU 4794 Arnold (斐波那契数模 n 的应用)
来源:互联网 发布:php断点续传原理 编辑:程序博客网 时间:2024/05/16 01:34
Arnold
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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?
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
112941
Sample Output
5720
题意:对一个N*N的矩阵进行若干次转换,每一次转换是矩阵的每一个像素(x,y) 会转移到((x+y)%N,(x+2*y)%N), 经过若干次转换会变回原来的矩阵,问最少要转换多少次才会变回原来的矩阵。
题解:http://pan.baidu.com/s/1dE0Tv5R 密码:usdy
AC代码:
#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <iostream>#include <algorithm>#include <functional>using namespace std;typedef unsigned long long LL;const int M = 2;struct Matrix //矩阵{ LL m[M][M];};Matrix A;Matrix I = {1, 0, 0, 1};Matrix multi(Matrix a, Matrix b, LL MOD) //矩阵的乘法{ Matrix c; for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) { c.m[i][j] = 0; for (int k = 0; k < M; k++) { c.m[i][j] = (c.m[i][j] % MOD + (a.m[i][k] % MOD) * (b.m[k][j] % MOD) % MOD) % MOD; } c.m[i][j] %= MOD; } } return c;}Matrix power(Matrix a, LL k, LL MOD) //矩阵的快速幂{ Matrix ans = I, p = a; while (k) { if (k & 1) { ans = multi(ans, p, MOD); k--; } k >>= 1; p = multi(p, p, MOD); } return ans;}LL gcd(LL a, LL b){ return b ? gcd(b, a % b) : a;}const int N = 400005;const int NN = 5005;LL num[NN], pri[NN];LL fac[NN];int cnt, c;bool prime[N]; //判断是否是素数int p[N]; //素数表int k; //素数个数void isprime() //打素数表{ k = 0; memset(prime, true, sizeof(prime)); for (int i = 2; i < N; i++) { if (prime[i]) { p[k++] = i; for (int j = i + i; j < N; j += i) { prime[j] = false; } } }}LL quick_mod(LL a, LL b, LL m) //快速幂的模运算{ LL ans = 1; a %= m; while (b) { if (b & 1) { ans = ans * a % m; b--; } b >>= 1; a = a * a % m; } return ans;}LL legendre(LL a, LL p) //赖让德公式 判断a是不是模P的二次剩余{ if (quick_mod(a, (p - 1) >> 1, p) == 1) { return 1; } else { return -1; }}void Solve(LL n, LL pri[], LL num[]) //将n质因数分解 pri存底数数,num存幂{ cnt = 0; LL t = (LL)sqrt(1.0 * n); for (int i = 0; p[i] <= t; i++) { if (n % p[i] == 0) { int a = 0; pri[cnt] = p[i]; while (n % p[i] == 0) { a++; n /= p[i]; } num[cnt] = a; cnt++; } } if (n > 1) //有余数 { pri[cnt] = n; num[cnt] = 1; cnt++; }}void Work(LL n) //n的所有因子{ c = 0; LL t = (LL)sqrt(1.0 * n); for (int i = 1; i <= t; i++) { if (n % i == 0) { if (i * i == n) { fac[c++] = i; } else { fac[c++] = i; fac[c++] = n / i; } } }}LL find_loop(LL n) //找循环节{ Solve(n, pri, num); LL ans = 1; for (int i = 0; i < cnt; i++) { LL record = 1; if (pri[i] == 2) { record = 3; } else if (pri[i] == 3) { record = 8; } else if (pri[i] == 5) { record = 20; } else { if (legendre(5, pri[i]) == 1) { Work(pri[i] - 1); } else { Work(2 * (pri[i] + 1)); } sort(fac, fac + c); for (int k = 0; k < c; k++) //判断因子中哪一个是循环节的长度 { Matrix a = power(A, fac[k] - 1, pri[i]); LL x = (a.m[0][0] % pri[i] + a.m[0][1] % pri[i]) % pri[i]; LL y = (a.m[1][0] % pri[i] + a.m[1][1] % pri[i]) % pri[i]; if (x == 1 && y == 0) { record = fac[k]; break; } } } for (int k = 1; k < num[i]; k++) { record *= pri[i]; } ans = ans / gcd(ans, record) * record; } return ans;}void Init(){ A.m[0][0] = 1; A.m[0][1] = 1; A.m[1][0] = 1; A.m[1][1] = 0;}int main(){ LL n; Init(); isprime(); while (cin >> n) { LL ans = find_loop(n); if (ans % 2 == 0) { ans /= 2; } cout << ans << endl; } return 0;}
0 0
- HDU 4794 Arnold (斐波那契数模 n 的应用)
- HDU 4794 Arnold (Fib数模 n 的应用)——2013 Asia Changsha Regional Contest
- HDU 4794 Arnold 斐波那契数列循环节
- HDU 3117 由n^k的前4位,推出(斐波那契)矩阵^k的前四位
- HDU 5620 KK's Steel(斐波那契数列的巧妙应用)
- HDU 5620 KK's Steel(找规律,斐波那契数列的应用)
- (hdu step 2.2.1)Fibonacci(求当n很大时的斐波那契数)
- 数模转换(ADC)的应用笔记(1)
- Fib数模n的循环节
- Fib数模n的循环节
- 斐波那契数列的应用
- 使用canvas 绘制斐波那契螺旋线(斐波那契数列的应用)
- 1242 . 斐波那契数列的第N项
- 斐波那契数列的第N项
- 输出斐波那契数列的前n项
- 斐波那契数列的前n项
- 1242 斐波那契数列的第N项
- 斐波那契数列的第n项。
- VTK根据几个点插值成样条曲线,然后以样条曲线为中心线,生成圆柱体管道
- FPGA开发流程1(详述每一环节的物理含义和实现目标)
- 【JZOJ 3839】Baby Step
- 148. Sort List**
- Delphi7下SuperObject的JSON使用方法
- HDU 4794 Arnold (斐波那契数模 n 的应用)
- leetcode oj java 337. House Robber III
- UGUI - Text控件
- 次幂函数取模算法
- Windows 串口通信简单示例
- 浅谈动感歌词-歌词显示篇
- 1027. Colors in Mars
- 怎样在VS2010中打开VS2012的项目
- 聊聊高并发系统之队列术