博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1005 Number Sequence zoj 1105
阅读量:7077 次
发布时间:2019-06-28

本文共 3288 字,大约阅读时间需要 10 分钟。

使用矩阵的方法解决zoj2105

结果出现了一下两种情况:

仅仅是改了一下求第n项,变为先求第n-1,n-2项,再求第n项!结果竟是不一样~~路过的大牛指导一下,谢谢!!

Wrong的代码:

/*  * zoj2105.c  *  *  Created on: 2011-9-20  *      Author: bjfuwangzhu */ #include
#define nmax 2 #define nnum 7 typedef struct matrix {
int num[nmax][nmax]; } matrix; matrix mul_matrix(matrix a, matrix b) {
matrix c; int i, j, k; for (i = 0; i < nmax; i++) {
for (j = 0; j < nmax; j++) {
c.num[i][j] = 0; for (k = 0; k < nmax; k++) {
c.num[i][j] += a.num[i][k] * b.num[k][j] % nnum; c.num[i][j] %= nnum; } } } return c; } void solve(int a, int b, int n) {
matrix temp, res; res.num[0][0] = 1, res.num[1][1] = 1, res.num[1][0] = res.num[0][1] = 0; temp.num[0][0] = a % nnum, temp.num[0][1] = b % nnum, temp.num[1][0] = 1, temp.num[1][1] = 0; while (n) {
if (n & 1) {
res = mul_matrix(res, temp); } temp = mul_matrix(temp, temp); n >>= 1; } printf("%d\n", res.num[0][0]); } int main() {
#ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif int a, b, n; while (~scanf("%d %d %d", &a, &b, &n)) {
if (a == 0 && b == 0 && n == 0) {
break; } if (n == 1 || n == 2) {
puts("1"); continue; } solve(a, b, n - 1); } return 0; }

Accept的代码:

/*  * zoj2105.c  *  *  Created on: 2011-9-20  *      Author: bjfuwangzhu */ #include
#define nmax 2 #define nnum 7 typedef struct matrix {
int num[nmax][nmax]; } matrix; matrix mul_matrix(matrix a, matrix b) {
matrix c; int i, j, k; for (i = 0; i < nmax; i++) {
for (j = 0; j < nmax; j++) {
c.num[i][j] = 0; for (k = 0; k < nmax; k++) {
c.num[i][j] += a.num[i][k] * b.num[k][j] % nnum; c.num[i][j] %= nnum; } } } return c; } void solve(int a, int b, int n) {
matrix temp, res; res.num[0][0] = 1, res.num[1][1] = 1, res.num[1][0] = res.num[0][1] = 0; temp.num[0][0] = a % nnum, temp.num[0][1] = b % nnum, temp.num[1][0] = 1, temp.num[1][1] = 0; while (n) {
if (n & 1) {
res = mul_matrix(res, temp); } temp = mul_matrix(temp, temp); n >>= 1; } printf("%d\n", (res.num[0][0] + res.num[0][1]) % nnum); } int main() {
#ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif int a, b, n; while (~scanf("%d %d %d", &a, &b, &n)) {
if (a == 0 && b == 0 && n == 0) {
break; } if (n == 1 || n == 2) {
puts("1"); continue; } solve(a, b, n - 2); } return 0; }

另一种写法,找规律:

#include
using namespace std; int main() {
int A, B, n, i, a[2000]; while (cin >> A >> B >> n) {
if (A == 0 && B == 0 && n == 0) break; if (n == 1 || n == 2) {
cout << "1" << endl; continue; } a[1] = 1; a[2] = 1; A %= 7; B %= 7; for (i = 3; i < 2000; i++) {
a[i] = (A * a[i - 1] + B * a[i - 2]) % 7; if (a[i - 1] == 1 && a[i] == 1) break; } i -= 2; n %= i; a[0] = a[i]; cout << a[n] << endl; } return 0; }

转载于:https://www.cnblogs.com/xiaoxian1369/archive/2011/09/20/2182835.html

你可能感兴趣的文章
【开发板技术支持】关于real6410 模拟摄像头与real6410 开发板的接线方式图
查看>>
设计模式代理
查看>>
Quagga 配置笔记
查看>>
同步处理(LockContext),期待大家的意见
查看>>
【HDOJ】4983 Goffi and GCD
查看>>
设置中的一些默认值
查看>>
HDU1556 color the ball(区间修改,单点查询)
查看>>
connect-flash 用法详解
查看>>
POJ 2486 Apple Tree(树形DP)
查看>>
洛谷P2168 荷马史诗 堆 哈夫曼树
查看>>
Java EE作业(二)
查看>>
让透明div里的文字不透明
查看>>
个人网站 www.ykmimi.com
查看>>
Python入门之文件基本操作和函数入门
查看>>
导入javax.servlet。伺服登记无法解决:The import javax.servlet.MultipartConfigElement cannot be resolved...
查看>>
hdu 2112 HDU Today (最短路)
查看>>
leetcode:Sort Colors (面试 笔试)
查看>>
数字1的数量
查看>>
java中super的作用
查看>>
java中你确定用对单例了吗?
查看>>