原创 數論基礎

歐幾里德算法 即求兩個數的最大公因子 int GCD(int a,int b) { if(b == 0) return a; else return GCD(b,a % b); } 擴展歐幾里德算法 它可以用來求解(a,

原创 組合數學基礎

n元素集合的循環r排列的數目是:                                                                           特別的,n個元素的循環排列的數目是 帕斯卡公式:    

原创 UVA - 11582 Colossal Fibonacci Numbers!(取模)

題意:輸入兩個非負整數a,b(0 <= a,b < 2^64)和 n(1<= n < 1000),輸出f(a^b) % n,f爲斐波那契數列。 思路:所有計算都是對n取模的,不妨設F(i) = f(i) % n,當f(i - 1),f(i

原创 POJ - 1061 青蛙的約會 (擴展歐幾里得找最小整數解)

題意:兩隻青蛙在圓圈上追趕,圓圈我們可以看作是一條首尾相接的數軸。設青蛙A的出發點座標是x,青蛙B的出發點座標是y。青蛙A一次能跳m米,青蛙B一次能跳n米,兩隻青蛙跳一次所花費的時間相同。總長L米。現在要你求出它們跳了幾次以後纔會碰面。

原创 POJ 3421 X-factor Chains(素數分解定理+組合數學)

題意:給出一個數X,找出1~X的因子序列,使得前一個數可以整除後一個數,求滿足條件的最大鏈長以及有多少個最長鏈 思路:根據素數分解定理:X=p1^a1 * p2^a2 * pn^an,最大鏈長等於a1 + a2 + ..... + an,

原创 POJ - 3292 Semi-prime H-numbers (埃氏篩思想)

題意:H數爲4n + 1這樣的數,H數的乘法是封閉的,H素數只能表示爲1和它本身的乘積,除了H素數,其餘的都是H合數,半素數是恰好能表示爲兩個H素數的乘積,給出一個數X,輸出這個數以及小於等於這個數的半素數的個數 思路:埃氏篩思想 #i

原创 [LeetCode ] Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

原创 [LeetCode ] House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed,

原创 [LeetCode ] Weekly Contest 113

 Largest Time for Given Digits Given an array of 4 digits, return the largest 24 hour time that can be made. The small

原创 [LeetCode ] Weekly Contest 112

Minimum Increment to Make Array Unique Given an array of integers A, a move consists of choosing any A[i], and increme

原创 [LeetCode ] Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and

原创 [LeetCode ] Best Time to Buy and Sell Stock系列題解

這一系列的題目的基本模型是給出n天每天股票的價格,可以在任意一天買股票,在之後的任意一天把股票賣出,賺取差價,限制是手中最多有一張股票,必選把手中的股票賣掉才能再買股票。求最大獲利。 題目1:Best Time to Buy and

原创 [LeetCode ] Basic Calculator

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and clos

原创 [LeetCode ] Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two no

原创 [LeetCode] Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb"