为什么要对1000000007取模(取余)

大数阶乘,大数的排列组合等,一般都要求将输出结果对1000000007取模(取余)

为什么总是1000000007呢= =

大概≖‿≖✧是因为:(我猜的,不服你打我呀~)

1. 1000000007是一个质数(素数),对质数取余能最大程度避免冲突~

2. int32位的最大值为2147483647,所以对于int32位来说1000000007足够大

3. int64位的最大值为2^63-1,对于1000000007来说它的平方不会在int64中溢出
所以在大数相乘的时候,因为(a∗b)%c=((a%c)∗(b%c))%c,所以相乘时两边都对1000000007取模,再保存在int64里面不会溢出

。◕‿◕。

sqrt()在POJ ACM中Compile Error编译错误的问题

POJ 3126-Prime Path-广度优先搜索bfs

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15440 Accepted: 8707

Description

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.

1033
1733
3733
3739
3779
8779
8179

The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

 

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

继续阅读POJ 3126-Prime Path-广度优先搜索bfs

POJ1426-Find The Multiple-深度优先搜索BFS

Find The Multiple
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24137 Accepted: 9997 Special Judge

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

继续阅读POJ1426-Find The Multiple-深度优先搜索BFS

poj1207-The 3n + 1 problem

 

POJ-2488 A Knights Journey-深度优先搜索DFS