diff --git a/Fibonacci in O(log(n)).cpp b/Fibonacci in O(log(n)).cpp new file mode 100644 index 0000000..b608341 --- /dev/null +++ b/Fibonacci in O(log(n)).cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +typedef long long ll; +typedef vector< vector > Matrix; +const int N = 2; +const ll mod = 1e9 + 7; +const Matrix mfib = {{1, 1}, {1, 0}}; +const Matrix midn = {{1, 0}, {0, 1}}; + +ll gcd(ll a, ll b) {return !a ? b : gcd(b%a, a);} +ll lcm(ll a, ll b) {return (a*b)/gcd(a, b);} + +unordered_map memo; + +inline Matrix mult(const Matrix& a, const Matrix& b, ll MOD = mod) { + Matrix ans = {{0, 0}, {0, 0}}; + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) { + ll sum = 0; + for(int k = 0; k < N; k++) { + sum += a[i][k]*b[k][j] % MOD; + } + ans[i][j] = sum; + } + } + return ans; +} + +inline ll fib(ll n) { + if(memo.count(n)) return memo[n]; + Matrix ans = midn, aux = mfib; + for(ll e = n; e; e >>= 1) { + if(e & 1) { + ans = mult(ans, aux); + } + aux = mult(aux, aux); + } + if(n) memo[n-1] = ans[1][1]; + memo[n+1] = ans[0][0]; + return memo[n] = ans[0][1]; +} + +int main() { + ios::sync_with_stdio(0), cin.tie(0); + ll n; + for(int TC = 1; cin >> n; TC++) { + printf("Case: %d\n", TC); + printf("fib(%lld) = %lld\n\n", n, fib(n)); + } + return 0; +} diff --git a/Reverse each word.c b/Reverse each word.c new file mode 100644 index 0000000..ed6917d --- /dev/null +++ b/Reverse each word.c @@ -0,0 +1,26 @@ +#include +#define swap(a, b) ((&(a) == &(b)) ? (a) : (a) ^= (b), (b) ^= (a), (a) ^= (b)) + +int main() { + int l, r, i; + char *str; + while(~scanf("%m[^\n]", &str)) { + for(l = r = i = 0; ; i++) { + if(str[i] == '\0') { + r = i - 1; + while(l < r) { + swap(str[l], str[r]); + l++, r--; + } + return 0 * puts(str); + } else if(str[i] == ' ') { + r = i - 1; + while(l < r) { + swap(str[l], str[r]); + l++, r--; + } + l = i + 1; + } + } + } +}