POJ2118-Firepersons

来源:互联网 发布:吐槽王pi知乎 编辑:程序博客网 时间:2024/05/22 14:21

Firepersons
Time Limit: 5000MS Memory Limit: 65536KTotal 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,...ak-1 and a recurrence relation an1<=i<=kan-ibi 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, , ak-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 60 

Sample Output

8

Source

CTU Open 2004


题意:告诉你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;}

原创粉丝点击