codeforces 227 C Flying Saucer Segments ,E Anniversary (矩阵快速幂)

目录

C.Flying Saucer Segments

E.Anniversary


C.Flying Saucer Segments

An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).

The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one — to the 1-st and the 3-rd ones, the 3-rd one — only to the 2-nd one. The transitions are possible only between the adjacent sections.

The spacecraft team consists of n aliens. Each of them is given a rank — an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.

Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.

At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.

Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.

Input

The first line contains two space-separated integers: n and m (1 ≤ n, m ≤ 109) — the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.

Output

Print a single number — the answer to the problem modulo m.

Examples

Input

1 10

Output

2

Input

3 8

Output

2

Note

In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.

To briefly describe the movements in the second sample we will use value , which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: ; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.

 

题意:有一个图,为  ①-②-③ ,有 n 个人,每个人有一个荣耀值,分别为 1 ~ n 。n 个人初始时在 ③ ,现在要走的 ①,一次只能走一个人,花费一分钟。一个人能够从 a 位置到 b 位置的条件是他的荣耀值在 a b 中是最大的。问所有人走到①需要多少步,答案 % m.

思路:假设有 n 个人,其答案是 a_{n},此时的状态是

当有 n + 1 个人的时候,花费 a_{n} 可到达的状态是:

那么剩下的就是荣耀值为 1 的人移动,很显然 1 先移至 ②,然后 ①的人在全部移动至 ③,1 再 移至 ①,然后 ③ 再移至 ①。

总花费: a_{n + 1} = a_{n}+1+a_{n}+1+a_{n}=3*a_{n}+2,利用矩阵快速幂即可求得答案。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;

const int maxn = 3;
LL mod;

struct mat{
    LL m[maxn][maxn],n; 	//n为输入的矩阵大小
    mat(LL _Size) {
        memset(m, 0, sizeof m);
        n = _Size;
    }
    inline void set() {
        memset(m, 0, sizeof m);
        for(int i = 1;i <= n;++ i) m[i][i] = 1;
    }
    inline mat operator * (const mat &B) const{	    // 计算矩阵 A * B, 放入C
        mat C = mat(n);
        for(int i = 1;i <= n;i ++)
            for(int j = 1;j <= n;j ++){
                for(int x = 1;x <= n;x ++)
                    C.m[i][j] = (C.m[i][j] + (m[i][x] * B.m[x][j]) % mod) % mod;
            }
        return C;
    }
    inline mat operator ^ (LL coun){	// 矩阵快速幂, 矩阵A的 coun次幂
        mat C = mat(n),A = *this;
        C.set();
        while(coun > 0){
            if(coun & 1) C = C * A;
            A = A * A;
            coun >>= 1;
        }
        return C;
    }
};

int main() {
    int n;
    while(~scanf("%d%lld",&n,&mod)){
        mat A(2);
        A.m[1][1] = 3; A.m[1][2] = 0;
        A.m[2][1] = 2; A.m[2][2] = 1;
        mat B(2);
        B.m[1][1] = 2; B.m[1][2] = 1;
        A = A ^ (n - 1);
        B = B * A;
        printf("%lld\n",B.m[1][1]);
    }
    return 0;
}

 

E.Anniversary

There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathematician Leonardo Fibonacci. Of course, such important anniversary needs much preparations.

Dima is sure that it'll be great to learn to solve the following problem by the Big Day: You're given a set A, consisting of numbers ll + 1, l + 2, ..., r; let's consider all its k-element subsets; for each such subset let's find the largest common divisor of Fibonacci numbers with indexes, determined by the subset elements. Among all found common divisors, Dima is interested in the largest one.

Dima asked to remind you that Fibonacci numbers are elements of a numeric sequence, where F1 = 1, F2 = 1, Fn = Fn - 1 + Fn - 2 for n ≥ 3.

Dima has more than half a century ahead to solve the given task, but you only have two hours. Count the residue from dividing the sought largest common divisor by m.

Input

The first line contains four space-separated integers mlr and k (1 ≤ m ≤ 109; 1 ≤ l < r ≤ 1012; 2 ≤ k ≤ r - l + 1).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Output

Print a single integer — the residue from dividing the sought greatest common divisor by m.

Examples

Input

10 1 8 2

Output

3

Input

10 1 8 3

Output

1

题意:给 m,l,r,k   ...   在下标为 l 到 r 之间的斐波那契数列中去取 k 个数,使得这 k 个数 gcd 最大。

思路:首先需要知道斐波那契数列的一个性质 gcd(F[a],F[b]]) = F[gcd(a,b)],所以题目有转化成了在 l 到 r 中取 k 个数,使其gcd 最大。  可以想到,枚举 1 ~ r 的数也就是枚举gcd,通过 r / i - (l - 1) / i >= k 来判断这个gcd是否合法。但是 r 特别大,所以这里是枚举 1 ~ √r , 然后用 r / i 来算另一半的数,同时判断两个即可。那么这样的复杂度就是 √r

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;

LL mod,l,r,k;

LL MaxGcd(){
    LL maxx = 1;
    for(LL i = 2;i <= (LL)sqrt(r * 1.0);++ i){
        if(r / i - (l - 1) / i >= k)
            maxx = max(maxx,i);
        LL another = r / i;
        if(r / another - (l - 1) / another >= k)
            maxx = max(maxx,another);
    }
    return maxx;
}

const int maxn = 3;
struct mat{
    LL m[maxn][maxn],n; 	//n为输入的矩阵大小
    mat(LL _Size) {
        memset(m, 0, sizeof m);
        n = _Size;
    }
    inline void set() {
        memset(m, 0, sizeof m);
        for(int i = 1;i <= n;++ i) m[i][i] = 1;
    }
    inline mat operator * (const mat &B) const{	    // 计算矩阵 A * B, 放入C
        mat C = mat(n);
        for(int i = 1;i <= n;i ++)
            for(int j = 1;j <= n;j ++){
                for(int x = 1;x <= n;x ++)
                    C.m[i][j] = (C.m[i][j] + (m[i][x] * B.m[x][j]) % mod) % mod;
            }
        return C;
    }
    inline mat operator ^ (LL coun){	// 矩阵快速幂, 矩阵A的 coun次幂
        mat C = mat(n),A = *this;
        C.set();
        while(coun > 0){
            if(coun & 1) C = C * A;
            A = A * A;
            coun >>= 1;
        }
        return C;
    }
};

int main() {
    while(cin >> mod >> l >> r >> k){
        LL maxx = MaxGcd(); debug(maxx);
        mat A(2);
        A.m[1][1] = 1; A.m[1][2] = 1;
        A.m[2][1] = 1; A.m[2][2] = 0;
        A = A ^ (maxx - 1);
        mat B(2);
        B.m[1][1] = 1; B.m[1][2] = 0;
        B = B * A;
        printf("%lld\n",B.m[1][1]);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值