UPC4248 [number theory] digital maze

Question J: [number theory] digital maze

Time limit: 1 Sec   Memory Limit: 128 MB
submit: 11   Resolution: 10
[ submit ] [ state ] [proposition man: ADMIN ]

Title Description

Cocoa small when the Museum of Science, see a collection, digital dense above, as follows:   
123,581,321,345,589 144 ...   
471,118,294,776,123 199 322 521 .. .   
. 6 1,016,264,263,110,178,288 466 754 ...   
915,243,963,102,165 267 432 699 ... 1131   
1,220,325,284,136,220,356 576 932 1508 ...   
1,423,376,097,157,254 41166510761741 ...   
17284573118191 50080913092118 309 ...   
19315081131212 55589814532351 343 ...   
22365894152246 39864410421686 ... 2728.   
25 4,166,107,173,280,453,733 1186 1919 ... 3105   
274,471,115,186,301 48778812752063 ... 3338   
...   
careful analysis, found quite regular.   
It turned out that the first line is the Fibonacci series. That is, in the row except for the first and the second number 1 and 2 respectively, the other numbers are adjacent to the left side and two numbers.   
Each subsequent row is also similar to the Fibonacci series. Only the first number of the i-th row before the row is the smallest integer not appear, while the second number associated with the first number and the row number of the line, i.e., A [i, 2] = A [ i, 1] * 2- (i -1). The smallest positive integer not appear in the first line is 4, the smallest integer not appear in the first three rows of nine. Therefore, beginning with the second line 4 and 7, and 9 and the fourth row start with 15.   
Small cocoa glad to take this discovery told the grandfather. Grandpa asked: Can you quote a line i, j-th column result that the number of modulo m is how much?   
Clever little cocoa by mental arithmetic will be able to know the answer. Can you write a program to solve it?

 

Entry

Each row has three positive integers, respectively, separated by a space, namely i, j and m. Wherein, i, j <= 1000000000, 2 <= m <= 10000.   

 

Export

I-th row corresponding to the output of each row, the positive integer m modulo result of j-th column.

 

Sample input

1 2 99

Sample Output

2 

found a very interesting topic, according to the meaning of the questions, our task is to focus on how to solve the first number of each line, while the second term of the recursive formula has been given, item j back, you can use fast direct solution matrix power.
I called the first column of the table to observe digital, you will find thirteen, increasing the number of 3,2,3,3,2,3,2,3,3,2,3,3,2 respectively, so we have these the number thirteen survive ok. One can not start the second question becomes water problem.
AC Code

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll mod;
struct mat
{
    ll a[2][2];
    mat operator *(const mat &t)const
    {
        mat res;
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<2;j++)
            {
                res.a[i][j]=0;
                for(int k=0;k<2;k++)
                    res.a[i][j]=(res.a[i][j]+a[i][k]*t.a[k][j]%mod)%mod;
            }
        }
        return res;
    }
};
mat qpow(mat a,ll b)
{
    mat ans;
    ans.a[0][0]=ans.a[1][1]=1;
    ans.a[0][1]=ans.a[1][0]=0;
    while(b)
    {
        if(b&1)ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
int nb[14]={0,3,2,3,3,2,3,2,3,3,2,3,3,2};
ll a,b;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll i,j;
    cin>>i>>j>>mod;
    for(int i=1;i<=13;i++)nb[i]+=nb[i-1];
    a=1+nb[13]*((i-1)/13)+nb[(i-1)%13];
    b=a*2-i+1;
    mat A,B;
    A.a[0][0]=1;
    A.a[0][1]=1;
    A.a[1][0]=1;
    B.a[0][0]=b;
    B.a[1][0]=a;
    A=qpow(A,j-2)*B;
    printf("%lld\n",A.a[0][0]);
    return 0;
}

 




Guess you like

Origin www.cnblogs.com/liuquanxu/p/11361934.html