湘潭大学OJ A.2016(大数取模/矩阵快速幂)

2016

Accepted : 71 Submit : 251
Time Limit : 2000 MS Memory Limit : 65536 KB 

2016

Given a 2×2  matrix

A=(a 11 a 21  a 12 a 22  ), 
find A n   where A 1 =A,A n =A×A n1   . As the result may be large, you are going to find only the remainder after division by 7  .

Special Note: The problem is intended to be easy. Feel free to think why the problem is called 2016 if you either:

  1. find it hard to solve;
  2. or, solved all the other problems easily.

Input

The input contains at most 40  sets. For each set:

The first line contains an integer n  ( 1n<10 100000   ).

The second line contains 2  integers a 11 ,a 12   .

The third line contains 2  integers a 21 ,a 22   .

( 0a ij <7  , (a 11 a 22 a 12 a 21 )  is not a multiple of 7  )

Output

For each set, a 2×2  matrix denotes the remainder of A n   after division by 7  .

Sample Input

2
1 1
1 2
2016
1 1
1 2

Sample Output

2 3
3 5
1 0
0 1
代码:

#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
    int m[3][3];
} ans,base;
char t[1000005];
int mod(char str[],int num)
{
    int flag = 0;
    for (int i = 0; str[i] != '\0'; i++)
        flag = ((flag * 10) + (str[i] - '0') ) % num;
    return flag;
}
node mulite(node a,node b)
{
    node temp;
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            temp.m[i][j]=0;
            for(int k=0; k<3; k++)
            {
                temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j])%7;
            }
        }
    }
    return temp;
}
int show(node a)
{
    printf("%d %d\n",a.m[0][0],a.m[0][1]);
    printf("%d %d\n",a.m[1][0],a.m[1][1]);
}
node quick(int t,node base)
{
    ans.m[0][0]=1;
    ans.m[0][1]=0;
    ans.m[1][0]=0;
    ans.m[1][1]=1;
    while(t)
    {
        if(t%2==1)
            ans=mulite(ans,base);
        base=mulite(base,base);
        t=t/2;
    }
    show(ans);
}
int main()
{

    while(~scanf("%s",t))
    {
        scanf("%d%d%d%d",&base.m[0][0],&base.m[0][1],&base.m[1][0],&base.m[1][1]);
        int we;
        we=mod(t,2016);
        quick(we,base);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值