A Simple Math Problem hdu1757
来源:互联网 发布:授受不亲 知乎 编辑:程序博客网 时间:2024/05/12 15:45
/* 这是一个典型的二分矩阵的问题,我弄了好久才弄出来,而且参考了2个人的代码;
我不知为何,我一开始构造的矩阵总是得不出正确的答案。
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9
1
1
1
1
1
1
1
1
1
0
9
8
7
6
5
4
3
2
1
0
这两个矩阵的乘积,在加一个单位矩阵,就通过了。
9 8 7 6 5 4 3 2 1 0
a0 1 1 1 1 1 1 1 1 1 1
a1
a2
a3
a4
a5
a6
a7
a8
a9
昨天想的是10 * 10 10*10
今天觉得好像1*10 10*10
的就可以了试下看,这道这个哪里出错了!!改成下面的就OK了,同时利用单位矩阵
a0 1
a1 1
a2 1
a3 1
a4 1
a5 1
a6 1
a7 1
a8 1
a9
感谢zhc给了灵感 也同时感谢网上给我纠错的网址,同时感谢师傅,给我找出了最初的错误
*/
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
const int MAX=10;
int mod;
struct Mat
{
int df[MAX][MAX];
}init, unit;
Mat Mul(Mat a,Mat b)
{
int i, j, k;
Mat c;
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
{
c.df[i][j] = 0;
for(k = 0; k < MAX; k++)
c.df[i][j] += a.df[i][k] * b.df[k][j];
if(c.df[i][j] >= mod)
c.df[i][j] %= mod;
}
return c;
}
Mat cal(int t)
{
Mat p, q;
p = init;
q = unit;
while(t != 1)
{
if(t&1)
{
p = Mul(p, q);
t--;
}
else
{
q = Mul(q, q);
t >>= 1;
}
}
p = Mul(p, q);
return p;
}
int main()
{
int i, j, k, sum;
Mat tt;
while(scanf("%d%d", &k, &mod) != EOF)
{
//memset(unit,0,sizeof(unit));
memset(unit.df,0,sizeof(unit.df));
memset(init.df,0,sizeof(init.df));
for(i = 0; i < MAX; i++)
{
scanf("%d",&unit.df[0][i]);
init.df[i][i] = 1;
if(i < MAX - 1)
unit.df[i+1][i] = 1;
}
k -= 9;
tt = cal(k);
sum = 0;
for(i = 0; i < MAX ; i++)
sum += tt.df[0][i]*(9 - i) % mod;
printf("%d/n", sum % mod);
}
return 0;
}
/*
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
const int MAX=10;
int mod;
struct Mat
{
long long int df[MAX][MAX];
}init, unit;
Mat Mul(Mat a,Mat b)
{
int i, j, k;
Mat c;
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
{
c.df[i][j] = 0;
for(k = 0; k < MAX; k++)
c.df[i][j] += a.df[i][k] * b.df[k][j];
if(c.df[i][j] >= mod)
c.df[i][j] %= mod;
}
return c;
}
Mat cal(int t)
{
Mat p, q;
p = init;
q = unit;
while(t != 1)
{
if(t&1)
{
p = Mul(p, q);
t--;
}
else
{
q = Mul(q, q);
t >>= 1;
}
}
p = Mul(p, q);
return p;
}
int main()
{
int i, j, k, sum;
Mat tt;
while(scanf("%d%d", &k, &mod) != EOF)
{
//memset(unit,0,sizeof(unit));
memset(unit.df,0,sizeof(unit.df));
memset(init.df,0,sizeof(init.df));
for(i = 0; i < MAX; i++)
{
scanf("%d",&unit.df[i][0]);
init.df[i][i] = 1;
//if(i != 0)
//unit.df[0][i] = 1;(这里想错了,矩阵构造好的话,要去试下的)
if(i < 9)
unit.df[i][i+1] = 1;
}
k -= 9;
tt = cal(k);
sum = 0;
for(i = 0; i < MAX ; i++)
sum += tt.df[i][0]*(9 - i) % mod;
printf("%d/n", sum % mod);
}
return 0;
}
- A Simple Math Problem hdu1757
- hdu1757 A Simple Math Problem
- HDU1757 A Simple Math Problem
- HDU1757 A Simple Math Problem
- hdu1757 - A Simple Math Problem
- A Simple Math Problem HDU1757
- HDU1757 A Simple Math Problem 矩阵应用
- hdu1757 A Simple Math Problem(矩阵乘法)
- hdu1757 A Simple Math Problem 矩阵乘法
- hdu1757---A Simple Math Problem(矩阵)
- Hdu1757 - A Simple Math Problem - 矩阵快速幂
- HDU1757-A Simple Math Problem(矩阵快速幂)
- hdu1757-- A Simple Math Problem(矩阵快速幂优化)
- hdu1757 - A Simple Math Problem 矩阵快速幂
- hdu1757 A Simple Math Problem(矩阵快速幂)
- hdu1757 A Simple Math Problem (矩阵快速幂)
- hdu1757 A Simple Math Problem(矩阵快速幂)
- [HDU1757]A Simple Math Problem(矩阵快速幂)
- C 学习笔记1
- 关于ADS1.2环境的安装
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
- 函数调用时,栈里到底发生了什么?
- sql 中IsNull(,)的使用
- A Simple Math Problem hdu1757
- 9.6Rest协议框架-bboss aop rpc
- 2010-3-20 CSDN博客公告区取消使用脚本的功能! 备份
- 掌握 Symbian 的重要类CEikonEnv
- linux_shell笔记(二)
- 使用MAP文件快速定位程序崩溃代码行
- paypal支付开发接口
- const用法小结
- 迷失的小鹿