ACM1005:Number Sequence

Problem Description

A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).

Input

The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output

For each test case, print the value of f(n) on a single line.

Sample Input

1 1 3
1 2 10
0 0 0

Sample Output

2
5

------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#define SIZE 2
long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE]);
int main()
{
	int A, B;
	long long int n;
	long long int factor[SIZE][SIZE];
	long long int ans[SIZE][SIZE];
 	while (scanf("%d%d%lld", &A, &B, &n) && (A||B||n))
	{
		memset(factor, 0, 2 * 2 * sizeof(int));
		memset(ans, 0, 2 * 2 * sizeof(int));
		factor[0][0] = A;
		factor[0][1] = B;
		factor[1][0] = 1;
		factor[1][1] = 0;
		//矩阵快速幂
		for (int i = 0; i < SIZE; i++)
			ans[i][i] = 1;
		n = n - 2;
		while (n > 0)
		{
			if (n & 1)
				memcpy(ans, multiple(ans, factor), 2 * 2 * sizeof(long long int));
			memcpy(factor, multiple(factor, factor), 2 * 2 * sizeof(long long int));
			n >>= 1;
		}
		printf("%lld\n", (ans[0][0] + ans[0][1]) % 7);
	}
	return 0;
}

long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE])
{
	int i, j, k;
	long long int c[SIZE][SIZE];
	memset(c, 0, 2 * 2 * sizeof(long long int));
	for(i = 0; i < SIZE; i++)
		for(j = 0; j < SIZE; j++)
			for (k = 0; k < SIZE; k++)
			{
				c[i][j] += a[i][k] * b[k][j];
				c[i][j] = c[i][j] % 7;
			}
	return c;
}

参考文章:

https://blog.csdn.net/codeswarrior/article/details/81258928
https://www.cnblogs.com/cmmdc/p/6936196.html

原文地址:https://www.cnblogs.com/mycodinglife/p/10520305.html

时间: 2024-10-12 00:07:46

ACM1005:Number Sequence的相关文章

1005:Number Sequence(hdu,数学规律题)

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU1711:Number Sequence

Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1]

hdu5014:number sequence对称思想

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5014 题目大意:给定数组 a[]={0,1,2......n} 求一个数组b[] 元素也为0.....n 但顺序与a[]不同 使得 sum(ai ^ bi)最大 注意到2^k =100000(k个0)  2^k-1 =11111(k个1) 那么 (2^k) ^ (2^k-1)=111111(k+1个1)等于 2^(k+1)-1  同样的有 (2^k+1) ^ (2^k-2)=2^(k+1)-1:

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

poj1019(Number Sequence)

题目地址:Number Sequence 题目大意: 有一串序列由阿拉伯数字够成11212312341234512345612345671234567812345678912345678910123456789101112345678910.......然后问你第几位数字是什么输出该数字. 解题思路: 排列组合.假如算第79位数字为多少,先计算出它在那个1-n的区间,可以看出79在1-12这段数字的区间,然后计算出前面1.1-2.1-3....1-11所有数的总和有多少位减去之后,再通过模拟在1

(KMP 1.1)hdu 1711 Number Sequence(KMP的简单应用——求pattern在text中第一次出现的位置)

题目: Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12902    Accepted Submission(s): 5845 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1

hdu1005 Number Sequence(找循环节)

题目链接: huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力打表,找循环节,但是也不是那么容易发现啊,所以这时候分析一下,因为最后都会mod7,所以总共有7X7总情况,即A 0,1,2,3,4,5,6,7,B也是如此,所以循环节为49,这么这个问题就解决了... 题目: Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memo

[AX]AX2012 Number sequence framework :(三)再谈Number sequence

AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal number包含公司代码和财务期间,比如这样的号码J-20-Jan11-000340,J表上Journal,20代表的是公司代码,Jan11为财务期间,000340才是系统生成的流水号. 在创建Numer sequence的界面上可以选择需要的Scope: Company and fiscal cale

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ