zoj 3729 2013长沙regional 求斐波那契数列模n循环节长度



http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5075


Arnold

Time Limit: 8 Seconds        Memory Limit: 65536 KB

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 (xy) 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 not more than 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
 
 
将题目中的坐标用数字代入,可以发现坐标组成了一个斐波那契数列。我们要求的就是斐波那契数列模N的循环节,求出这个循环节后,再除以二,就是本题的答案了。

http://blog.csdn.net/acdreamers/article/details/10983813
一坑是unsigned long long 才能存下
//#include<bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>

using namespace std;

#define For(i,a,b) for(int (i)=(a);(i) < (b);(i)++)
#define rof(i,a,b) for(int (i)=(a);(i) > (b);(i)--)
#define IOS ios::sync_with_stdio(false)
#define lson l,m,rt <<1
#define rson m+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))

//typedef long long ll;
typedef unsigned long long ll;


void RI (int& x){
    x = 0;
    char c = getchar ();
    while (c == ' '||c == '\n')    c = getchar ();
    bool flag = 1;
    if (c == '-'){
        flag = 0;
        c = getchar ();
    }
    while (c >= '0' && c <= '9'){
        x = x * 10 + c - '0';
        c = getchar ();
    }
    if (!flag)    x = -x;
}
void RII (int& x, int& y){RI (x), RI (y);}
void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}

/**************************************END define***************************************/
const int maxn = 1e5+10;
const int maxm= 2e4+10;
const int INF =0x3f3f3f3f;
ll pri[maxm],num[maxm],fac[maxm];
bool flag[maxn];

vector <int> prime;
//freopen("input.txt","r",stdin);
const int M =2;

struct Matrix
{
    ll m[M][M];
};
Matrix A;
Matrix l={1,0,0,1};
void init(){
    A.m[0][0]=1;
    A.m[0][1]=1;
    A.m[1][0]=1;
    A.m[1][1]=0;
}


Matrix multi(Matrix a, Matrix b, ll MOD)
{
    Matrix c;
    For(i,0,M){
        For(j,0,M){
            c.m[i][j]=0;
            for(int k=0;k<M;k++){
                c.m[i][j] +=a.m[i][k]*b.m[k][j]%MOD;
                c.m[i][j]%=MOD;
            }
            
        }
    }
    return c;
}
Matrix power(Matrix a,ll b, ll MOD)
{
    Matrix ans =l;
    while(b){
        if(b&1)  ans=multi(ans,a,MOD);
        b>>=1;
        a=multi(a,a,MOD);
    }
    return ans;
}

ll quick_mod(ll a, ll b, ll m){
    ll ans=1;
    while(b){
        if(b&1) ans=(ans*a)%m;
        b>>=1;
        a=(a*a)%m;
    }
    return ans;
}
ll gcd(ll a, ll b){
    return b==0?a:gcd(b,a%b);
}
//勒让德公式
ll legendre(ll a,ll p){
    if(quick_mod(a,(p-1)/2,p)==1) return 1;
    else return 0;
}
void get_prime(){
    mem(flag,0);
    for(int i=2;i<maxn;i++)
        if(!flag[i]){
            prime.push_back(i);
            for(int j=0;j<prime.size()&&(ll)i*prime[j]<(ll)maxn;j++){
                flag[i*prime[j]]=true;
            }
        }

}
//把n素因子分解
ll fenjie(ll n , ll pri[], ll num[])
{
    ll cnt=0;
    for(int i=0;prime[i]<=(ll)sqrt(n+0.5);i++){
        if(n%prime[i]==0){
            int a=0;
            pri[cnt]=prime[i];
            while(n%prime[i]==0){
                a++;
                n/=prime[i];
            }
            num[cnt++]=a;
        }
    }
    if(n>1){
        pri[cnt]=n;
        num[cnt++]=1;
    }
    return cnt;
}
//fac存n所有的因子
int Work(ll n){
    int c=0;
    for(ll i=1;i<=(ll)sqrt(n+0.5);i++){
        if(n%i==0){
            if(i*i==n) fac[c++]=i;
            else{
                fac[c++]=i;
                fac[c++]=n/i;
            }
        }
    }
    return c;
}
ll findloop(ll n){
    ll cnt=fenjie(n,pri,num);
    ll ans=1;
    for(ll 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{
            ll c;
            if(legendre(5,pri[i])==1){
                c=Work(pri[i]-1);
            }
            else c=Work((pri[i]+1)*2);
            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;
}
int main()
{
    //freopen("input.txt","r",stdin);
    init();
    get_prime();
    ll n;
    while(cin>>n)
    {
        ll ans=findloop(n);
        if(ans%2 ==0) ans=ans/2;
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值