HDU2256-Problem of Precision(矩陣構造+快速冪),冪零矩陣

來源:互聯網
上載者:User

HDU2256-Problem of Precision(矩陣構造+快速冪),冪零矩陣

題目連結


題意:求sqrt(sqrt(2) + sqrt(3)) ^ 2n MOD 1024

思路:


代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MOD = 1024;int n;struct mat {    int s[2][2];    mat(int a = 0, int b = 0, int c = 0, int d = 0) {        s[0][0] = a;         s[0][1] = b;         s[1][0] = c;         s[1][1] = d;     }    mat operator * (const mat& c) {        mat ans;         sizeof(ans.s, 0, sizeof(ans.s));        for (int i = 0; i < 2; i++)             for (int j = 0; j < 2; j++)                for (int k = 0; k < 2; k++)                    ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD;        return ans;    }}tmp(5, 12, 2, 5);mat pow_mod(int k) {    if (k == 1)        return tmp;    mat a = pow_mod(k / 2);    mat ans = a * a;    if (k % 2)        ans = ans * tmp;    return ans;}int main() {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d", &n);         mat ans = pow_mod(n);         printf("%d\n", (ans.s[0][0] * 2 - 1) % MOD);     }    return 0;}



pascal的快速冪的矩陣乘法,詳解與具體實現

給你寫個架構吧,快速冪就是二分遞迴

function quick(var x:array[1..2,1..2] of integer); 根據你自己的矩陣大小改變
var y:array[1..2,1..2]of integer;

begin
if n=1 then exit(a); a為原基礎矩陣。
y:=quick(n div 2);
if n mod 2=0 then exit(jucheng(y,y)) jucheng就是矩乘的函數
else exit(jucheng(jucheng(y,y),a));
end;
 
構造一個3*3的拉丁方陣,使該矩陣中的每行每列中數字1,2,3都只出現一次30分不成敬意

根本不止3種情況,應該有12種。
3 2 1
2 1 3
1 3 2

3 2 1
1 3 2
2 1 3

2 1 3
3 2 1
1 3 2

2 1 3
1 3 2
3 2 1

1 3 2
3 2 1
2 1 3

1 3 2
2 1 3
3 2 1

1 2 3
2 3 1
3 1 2

1 2 3
3 1 2
2 3 1

2 3 1
1 2 3
3 1 2

2 3 1
3 1 2
1 2 3

3 1 2
1 2 3
2 3 1

3 1 2
2 3 1
1 2 3
程式如下:
#include <stdio.h>
#include <string.h>

void init(int seed[3], int a[3][3])
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
a[i][j] = seed[(i+j)%3];
}
}
}
void print(int x[3][3])
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
printf("%d ", x[i][j]);
}
printf("\n");
}
}
void show(int seed[3])
{
int x[3][3];
int a[3][3];
int i, j, k;
init(seed, a);
for(i=0; i<3; i++){
for(j=0; j<3; j++){
if(j!=i){
k=3-i-j;
memcpy(x[0],a[i],sizeof(int[3]));
memcpy(x[1],a[j],sizeof(int[3]));
memcpy(x[2],a[k],sizeof(int[3]));
print(x);
printf("\n");
}
}
}
}
void main()
{
int i;
int asc[3]={1,2,3}, dsc[3]={3,2,1};
int matrix[3][3];
show(dsc);
show(asc);
}...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.