POJ2118-Firepersons

Firepersons
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1153 Accepted: 512

Description

The Association for Courtly Manners, an international organization for standardization of social interactions (Better known under the name Absurdly Clumsy Moralists, but let's not take prejudice.) has decided to create a new international standard defining ranks of firepersons (Formerly firemen, but the international standards of course must be politically correct.) - each fireperson receives an integer number describing his rank and when they arrive to a fire, they must enter the fire ground in order of increasing ranks and the low ranked firepersons must keep the fire burning long enough for the high ranked firepersons to enjoy extinguishing sufficiently. 

The ranks are assigned according to an Arbitrary Constant Multiplier Sequence. An ACM-sequence of order k is an integer sequence defined by its first k terms a0, a1,...a k-1 and a recurrence relation a n1<=i<=ka n-ib i mod 10 000 for n >= k, where the bi's are integer constants. The i-th oldest fireperson then gets rank ai. 

Your task is to calculate the rank of the i-th fireperson, given parameters of the ACM-sequence and the number i. 

Input

The input consists of several instances. Each instance is described on a single line containing the following integers separated by a single space: k, a0, , a k-1, b1, , bk, i. Here 1 <= k <= 100 is the order of the sequence, 0 <= ai < 10 000 are the first k elements of the sequence, 0 <= bi < 10 000 are the multipliers and 0 <= i < 1 000 000 000 is the number of the element we ask for. 

The input ends with a line containing a number 0. 

Output

The output consists of several lines corresponding to the instances on the input. The l-th line contains a single integer ai which is the i-th element of the sequence described by the l-th input instance.

Sample Input

2 0 1 1 1 6
0 

Sample Output

8

Source



题意:告诉你a0,a1......ak-1和b0,b1......bk-1,根据题目的式子,算出an

解题思路:矩阵快速幂即可


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <map>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;

struct Matrix
{
	LL v[105][105];
	Matrix()
	{
		memset(v, 0, sizeof v);
	}
} dan;

Matrix mul(Matrix a, Matrix b, int d)
{
	Matrix ans;
	for (int i = 0; i < d; i++)
		for (int j = 0; j < d; j++)
			for (int k = 0; k < d; k++)
				ans.v[i][j] = (ans.v[i][j] + a.v[i][k] * b.v[k][j] % 10000) % 10000;
	return ans;
}

Matrix pow(Matrix a, int k, int d)
{
	Matrix ans = dan;
	while (k)
	{
		if (k & 1) ans = mul(ans, a, d);
		k >>= 1;
		a = mul(a, a, d);
	}
	return ans;
}

int main()
{
	int k, n;
	while (~scanf("%d", &k) && k)
	{
		for (int i = 0; i < k; i++) scanf("%d", &dan.v[0][k - i - 1]);
		Matrix a, ans;
		for (int i = 0; i < k; i++) scanf("%d", &a.v[i][0]);
		for (int i = 1; i < k; i++) a.v[i - 1][i] = 1;
		scanf("%d", &n);
		if (n < k)
		{
			printf("%d\n", dan.v[0][k - n - 1] % 10000);
			continue;
		}
		ans = pow(a, n - k + 1, k);
		printf("%lld\n", ans.v[0][0] % 10000);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值