POJ 3070-Fibonacci

POJ 3070-Fibonacci

Fibonacci

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

Output

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input

0
9
999999999
1000000000
-1

Sample Output

0
34
626
6875

Hint

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

.

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

.

SourceStanford Local 2006

$$$$f(n)=\begin{cases} 0, & n=0;\ 1, & n=1;\ f(n-1)+f(n-2), & n>1.\end{cases}$$$$

typedef long long LL;
LL Fib1(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
return Fib1(n – 1) + Fib1(n – 2);
}

这种算法的时间复杂度是 $O(2^n)$。 但是这种解法有很多重复计算，比如算f(8)=f(7)+f(6)时，f(7)重复计算了f(6)。所以我们可以从小开始算起，把f(n)的前两个值保存下来，每次滚动赋值，代码如下：

LL Fib2(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
LL fn_2 = 0, fn_1 = 1, ans = 0;
for (int i = 2; i <= n; ++i) {
ans = fn_1 + fn_2;
fn_2 = fn_1;
fn_1 = ans;
}
return ans;
}

const int MAXN = 2;
vector<vector<LL> > multi(const vector<vector<LL> >& mat1, const vector<vector<LL> >& mat2)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i) {
for (int j = 0; j < MAXN; ++j) {
for (int k = 0; k < MAXN; ++k) {
mat[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
return mat;
}
vector<vector<LL> > fastPow(vector<vector<LL> >& mat1, int n)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i)
mat[i][i] = 1;
while (n != 0) {
if (n & 1)
mat = multi(mat, mat1);
mat1 = multi(mat1, mat1);
n >>= 1;
}
return mat;
}
LL Fib3(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
vector<vector<LL> > mat = { { 1, 1 }, { 1, 0 } };
vector<vector<LL> > matN = fastPow(mat, n);
return matN[0][1];
}

POJ这题的代码如下，古老的POJ居然不支持第37行的vector初始化，崩溃。

#include <iostream> #include<cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 2;
const int MOD = 10000;
vector<vector<LL> > multi(const vector<vector<LL> >& mat1, const vector<vector<LL> >& mat2)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i) {
for (int j = 0; j < MAXN; ++j) {
for (int k = 0; k < MAXN; ++k) {
mat[i][j] = (mat[i][j] + mat1[i][k] * mat2[k][j]) % MOD;
}
}
}
return mat;
}
vector<vector<LL> > fastPow(vector<vector<LL> >& mat1, int n)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i)
mat[i][i] = 1;
while (n != 0) {
if (n & 1)
mat = multi(mat, mat1);
mat1 = multi(mat1, mat1);
n >>= 1;
}
return mat;
}
LL Fib3(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
//vector<vector<LL>> mat = { { 1,1 },{ 1,0 } }; //———————–
vector<vector<LL> > mat;
vector<LL> r1;
r1.push_back(1), r1.push_back(1);
vector<LL> r2;
r2.push_back(1), r2.push_back(0);
mat.push_back(r1);
mat.push_back(r2); //———————-
vector<vector<LL> > matN = fastPow(mat, n);
return matN[0][1];
}
int main()
{
int n;
while (scanf("%d", &n) && n != -1) {
LL ans = Fib3(n);
printf("%d\n", ans % MOD);
}
return 0;
}

POJ 2369-Permutations

POJ 2369-Permutations Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2792 Accepted: 1473 Description We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Less formally, that is a way to reorder elements of the set. For example, one can define a permutation of the set {1,2,3,4,5} as follows: This record defines a permutation P as follows: P(1) = 4, P(2) = 1, P(3) = 5, etc. What is the value of the expression P(P(1))? It’s clear, that P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3. One can easily see that if P(n) is a permutation then P(P(n)) is a permutation as well. In our example (believe us) It is natural to denote this permutation by P2(n) = P(P(n)). In a general form the defenition is as follows: P(n) = P1(n), Pk(n) = P(Pk-1(n)). Among the permutations there is a very important one — that moves nothing: It is clear that for every k the following relation is satisfied: (EN)k = EN. The following less trivial statement is correct (we won’t prove it here, you may prove it yourself incidentally): Let P(n) be some permutation of an N elements set. Then there exists a natural number k, that Pk = EN. The least natural k such that Pk = EN is called an order of the permutation P. The problem that your program should solve is formulated now in a very simple manner: “Given a permutation find its order.” Input In the first line of the standard input an only natural number N (1 <= N <= 1000) is contained, that is a number of elements in the set that is rearranged by this permutation. In the second line there are N natural numbers of the range from 1 up to N, separated by a space, that define a permutation — the numbers P(1), P(2),…, P(N). Output You should write an only natural number to the standard output, that is an order of the permutation. You may consider that an answer shouldn’t exceed 109. Sample Input 5 4 1 5 2 3 Sample Output 6 Source Ural State University Internal Contest October’2000 Junior Session

POJ 2352-Stars

POJ 2352-Stars Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36453 Accepted: 15852 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it’s formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. You are to write a program that will count the amounts of the stars of each level on a given map. Input The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate. Output The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1. Sample Input 5 1 1 5 1 7 1 3 3 5 5 Sample Output 1 2 1 1 Hint This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. Source Ural Collegiate Programming Contest 1999

POJ 1386-Play on Words

POJ 1386-Play on Words Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10039 Accepted: 3441 Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word “acm” can be followed by the word “motorola”. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door. Input The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list. Output Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times. If there exists such an ordering of plates, your program should print the sentence “Ordering is possible.”. Otherwise, output the sentence “The door cannot be opened.”. Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The door cannot be opened. Ordering is possible. The door cannot be opened. Source Central Europe 1999

POJ 1061-青蛙的约会

POJ 1061-青蛙的约会 青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 92723 Accepted: 17043 Description 两只青蛙在网上相识了，它们聊得很开心，于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上，于是它们约定各自朝西跳，直到碰面为止。可是它们出发之前忘记了一件很重要的事情，既没有问清楚对方的特征，也没有约定见面的具体位置。不过青蛙们都是很乐观的，它们觉得只要一直朝着某个方向跳下去，总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上，不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙，你被要求写一个程序来判断这两只青蛙是否能够碰面，会在什么时候碰面。 我们把这两只青蛙分别叫做青蛙A和青蛙B，并且规定纬度线上东经0度处为原点，由东往西为正方向，单位长度1米，这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x，青蛙B的出发点坐标是y。青蛙A一次能跳m米，青蛙B一次能跳n米，两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。 Input 输入只包括一行5个整数x，y，m，n，L，其中x≠y < 2000000000，0 < m、n < 2000000000，0 < L < 2100000000。 Output 输出碰面所需要的跳跃次数，如果永远不可能碰面则输出一行”Impossible” Sample Input 1 2 3 4 5 Sample Output 4 Source 浙江

POJ 1163-The Triangle

POJ 1163-The Triangle The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38525 Accepted: 23126 Description Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right. Input Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99. Output Your program is to write to standard output. The highest sum is written as an integer. Sample Input 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 Sample Output 30 Source IOI 1994

DP水题一个，如果将等边三角形转换成Sample Input里的等腰直角三角形，设则某一点的路径和为sum[i][j]，则因为只能从(i-1,j)和(i-1,j-1)这两个点到达(i,j)，所以sum[i][j]=max{sum[i-1][j],sum[i-1][j-1]}+triangle[i][j]。如果为了节省空间，j可以从i downto 0循环，也就是DP表从右往左填，这样只需要一行空间。 不多说，简单题，直接上代码。 [cpp] #include<iostream> using namespace std; const int MAX_N=101; int triangle[MAX_N][MAX_N]; int sum[MAX_N]={0}; int DP(int n) { int rs=0; for(int i=0;i<n;i++) { for(int j=i;j>=0;j–)//从右往左填表，只需一行空间 { if(j>0) { sum[j]=(sum[j]>sum[j-1]?sum[j]:sum[j-1])+triangle[i][j]; } else if(j==0) { sum[j]+=triangle[i][j]; } if(sum[j]>rs)//记录最大值 rs=sum[j]; } } /*int rs=0; for(int i=0;i<n;i++) if(sum[i]>rs) rs=sum[i];*/ return rs; } int main() { //freopen("input.txt","r",stdin); int n; cin>>n; for(int i=0;i<n;i++) for(int j=0;j<=i;j++) cin>>triangle[i][j]; cout<<DP(n); return 0; } [/cpp] 本代码提交AC，用时0MS,内存260K。 ]]>

POJ 1330-Nearest Common Ancestors

POJ 1330-Nearest Common Ancestors Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19432 Accepted: 10292 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,…,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is. For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y. Write a program that finds the nearest common ancestor of two distinct nodes in a tree. Input The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,…, N. Each of the next N -1 lines contains a pair of integers that represent an edge –the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N – 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed. Output Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor. Sample Input 2 16 1 14 8 5 10 16 5 9 4 6 8 4 4 10 1 13 6 15 10 11 6 7 10 2 16 3 8 1 16 12 16 7 5 2 3 3 4 3 1 1 5 3 5 Sample Output 4 3 Source Taejon 2002

POJ 1080-Human Gene Functions

POJ 1080-Human Gene Functions Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17334 Accepted: 9637 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their functions, because these can be used to diagnose human diseases and to design new drugs for them. A human gene can be identified through a series of time-consuming biological experiments, often with the help of computer programs. Once a sequence of a gene is obtained, the next job is to determine its function. One of the methods for biologists to use in determining the function of a new gene sequence that they have just identified is to search a database with the new gene as a query. The database to be searched stores many gene sequences and their functions – many researchers have been submitting their genes and functions to the database and the database is freely accessible through the Internet. A database search will return a list of gene sequences from the database that are similar to the query gene. Biologists assume that sequence similarity often implies functional similarity. So, the function of the new gene might be one of the functions that the genes from the list have. To exactly determine which one is the right one another series of biological experiments will be needed. Your job is to make a program that compares two genes and determines their similarity as explained below. Your program may be used as a part of the database search if you can provide an efficient one. Given two genes AGTGATG and GTTAG, how similar are they? One of the methods to measure the similarity of two genes is called alignment. In an alignment, spaces are inserted, if necessary, in appropriate positions of the genes to make them equally long and score the resulting genes according to a scoring matrix. For example, one space is inserted into AGTGATG to result in AGTGAT-G, and three spaces are inserted into GTTAG to result in –GT–TAG. A space is denoted by a minus sign (-). The two genes are now of equal length. These two strings are aligned: AGTGAT-G -GT–TAG In this alignment, there are four matches, namely, G in the second position, T in the third, T in the sixth, and G in the eighth. Each pair of aligned characters is assigned a score according to the following scoring matrix. ![](http://poj.org/images/1080/1080_1.gif) denotes that a space-space match is not allowed. The score of the alignment above is (-3)+5+5+(-2)+(-3)+5+(-3)+5=9. Of course, many other alignments are possible. One is shown below (a different number of spaces are inserted into different positions): AGTGATG -GTTA-G This alignment gives a score of (-3)+5+5+(-2)+5+(-1) +5=14. So, this one is better than the previous one. As a matter of fact, this one is optimal since no other alignment can have a higher score. So, it is said that the similarity of the two genes is 14. Input The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each test case consists of two lines: each line contains an integer, the length of a gene, followed by a gene sequence. The length of each gene sequence is at least one and does not exceed 100. Output The output should print the similarity of each test case, one per line. Sample Input 2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA Sample Output 14 21 Source Taejon 2001

POJ 1094-Sorting It All Out

POJ 1094-Sorting It All Out Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28397 Accepted: 9814 Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not. Input Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character “<” and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input. Output For each problem instance, output consists of one line. This line should be one of the following three: Sorted sequence determined after xxx relations: yyy…y. Sorted sequence cannot be determined. Inconsistency found after xxx relations. where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy…y is the sorted, ascending sequence. Sample Input 4 6 A<B A<C B<C C<D B<D A<B 3 2 A<B B<A 26 1 A<Z 0 0 Sample Output Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined. Source East Central North America 2001

POJ 2263-Heavy Cargo

POJ 2263-Heavy Cargo Heavy Cargo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3549 Accepted: 1894 Description Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive. Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities. Input The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network. Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 – 10000. Roads can always be travelled in both directions. The last line of the test case contains two city names: start and destination. Input will be terminated by two values of 0 for n and r. Output For each test case, print three lines: a line saying “Scenario #x” where x is the number of the test case a line saying “y tons” where y is the maximum possible load a blank line Sample Input 4 3 Karlsruhe Stuttgart 100 Stuttgart Ulm 80 Ulm Muenchen 120 Karlsruhe Muenchen 5 5 Karlsruhe Stuttgart 100 Stuttgart Ulm 80 Ulm Muenchen 120 Karlsruhe Hamburg 220 Hamburg Muenchen 170 Muenchen Karlsruhe 0 0 Sample Output Scenario #1 80 tons Scenario #2 170 tons Source Ulm Local 1998