Last active
March 10, 2016 02:19
-
-
Save froghramar/c11cbb1717c91630f4fd to your computer and use it in GitHub Desktop.
Coding Help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Bigint { | |
string a; | |
int sign; | |
Bigint(){} | |
Bigint(string b){ | |
*this = b; | |
} | |
int size(){ | |
return a.size(); | |
} | |
Bigint inverseSign(){ | |
sign *= -1; | |
return (*this); | |
} | |
Bigint normalize(int newSign){ | |
for(int i = a.size() - 1; i > 0 && a[i] == '0'; i--) | |
a.erase(a.begin() + i); | |
sign = (a.size() == 1 && a[0] == '0') ? 1 : newSign; | |
return *this; | |
} | |
void operator = (string b){ | |
a = b[0] == '-' ? b.substr(1) : b; | |
reverse(a.begin(),a.end()); | |
this->normalize(b[0] == '-' ? -1 : 1); | |
} | |
bool operator < ( const Bigint &b ) const{ | |
if(sign != b.sign) return sign < b.sign; | |
if(a.size() != b.a.size()) | |
return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size(); | |
for(int i = a.size() - 1; i >= 0; i--) | |
if(a[i] != b.a[i]) | |
return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i]; | |
return false; | |
} | |
bool operator == ( const Bigint &b ) const{ | |
return a == b.a && sign == b.sign; | |
} | |
Bigint operator + (Bigint b){ | |
if( sign != b.sign ) return (*this) - b.inverseSign(); | |
Bigint c; | |
for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ){ | |
carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0); | |
c.a += (carry % 10 + 48); | |
carry /= 10; | |
} | |
return c.normalize(sign); | |
} | |
Bigint operator - (Bigint b){ | |
if(sign != b.sign) return (*this) + b.inverseSign(); | |
int s = sign; | |
sign = b.sign = 1; | |
if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s); | |
Bigint c; | |
for( int i = 0, borrow = 0; i < a.size(); i++ ){ | |
borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48); | |
c.a += borrow >= 0 ? borrow + 48 : borrow + 58; | |
borrow = borrow >= 0 ? 0 : 1; | |
} | |
return c.normalize(s); | |
} | |
Bigint operator * (Bigint b){ | |
Bigint c("0"); | |
for(int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48){ | |
while(k--) c = c + b; | |
b.a.insert(b.a.begin(), '0'); | |
} | |
return c.normalize(sign * b.sign); | |
} | |
Bigint operator / (Bigint b){ | |
if(b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48); | |
Bigint c("0"), d; | |
for(int j = 0; j < a.size(); j++) d.a += "0"; | |
int dSign = sign * b.sign; | |
b.sign = 1; | |
for(int i = a.size() - 1; i >= 0; i--){ | |
c.a.insert( c.a.begin(), '0'); | |
c = c + a.substr(i, 1); | |
while( !( c < b ) ) c = c - b, d.a[i]++; | |
} | |
return d.normalize(dSign); | |
} | |
Bigint operator % (Bigint b){ | |
if(b.size() == 1 && b.a[0] == '0') b.a[0] /= (b.a[0] - 48); | |
Bigint c("0"); | |
b.sign = 1; | |
for(int i = a.size() - 1; i >= 0; i--){ | |
c.a.insert( c.a.begin(), '0'); | |
c = c + a.substr(i, 1); | |
while( !( c < b ) ) c = c - b; | |
} | |
return c.normalize(sign); | |
} | |
void print(){ | |
if(sign == -1 ) putchar('-'); | |
for(int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long long bigmod(long long n, long long p) { | |
if(p == 0) return 1; | |
else if(p & 1) return (n * bigmod(n, p - 1)) % mod; | |
else{ | |
long long x = bigmod(n, p / 2); | |
return (x * x) % mod; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Catalan Series : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, ................. | |
Nth Catalan number, Cat(n) = (2n C n) / (n + 1) | |
Recursive relation, Cat(n) = sum ( C(i) * C(n-i) ), where i in [0,n] | |
Cat(n) = Cat(n-1) * (4n - 2) / (n + 1) | |
*/ | |
long long cat[N]; | |
void precalc(){ | |
cat[0] = 1; | |
for(int i = 1; i < N; i++){ | |
cat[i] = 0; | |
for(int j = 0; j < i; j++){ | |
cat[i] = cat[i] + cat[j] * cat[i-j-1]; | |
} | |
} | |
} | |
long long cat[N]; | |
void precalc(){ | |
cat[0] = 1; | |
for(int i = 1; i < N; i++){ | |
cat[i] = cat[i-1] * (4 * i - 2) / ( i + 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
import java.io.BufferedReader; | |
import java.math.BigInteger; | |
public class FastIO{ | |
static InputReader in; | |
static PrintWriter out; | |
static class Task { | |
public void solve(InputReader in, PrintWriter out) throws FileNotFoundException { | |
} | |
} | |
public static void main(String[] args) throws FileNotFoundException{ | |
fread(); | |
//sysread(); | |
Task solver = new Task(); | |
solver.solve(in, out); | |
out.close(); | |
} | |
public static void fread() throws FileNotFoundException { | |
in = new InputReader(new FileInputStream("input.txt")); | |
out = new PrintWriter(new FileOutputStream("output.txt")); | |
} | |
public static void sysread() throws FileNotFoundException { | |
in = new InputReader(System.in); | |
out = new PrintWriter(System.out); | |
} | |
static class InputReader { | |
public BufferedReader reader; | |
public StringTokenizer tokenizer; | |
public InputReader(InputStream stream) { | |
reader = new BufferedReader(new InputStreamReader(stream), 32768); | |
tokenizer = null; | |
} | |
public String next() { | |
while (tokenizer == null || !tokenizer.hasMoreTokens()) { | |
try { | |
tokenizer = new StringTokenizer(reader.readLine()); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
return tokenizer.nextToken(); | |
} | |
public int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
public long nextLong() { | |
return Long.parseLong(next()); | |
} | |
public BigInteger nextBigInteger(){ | |
return new BigInteger(next()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Mobius(){ | |
mu[1] = 1; | |
for(int i = 1; i < N; i++){ | |
if(mu[i]){ | |
for(int j = i + i; j < N; j += i){ | |
mu[j] -= mu[i]; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Schroeder Series : 1, 1, 3, 11, 45, 197, 903, 4279, 20793, 103049, 518859, 2646723, 13648869, 71039373,............. | |
Recursive relation, Sch(n) = ( (6n - 3) * Sch(n - 1) - (n - 2) * Sch(n - 2) ) / (n + 1) | |
*/ | |
long long sch[N]; | |
void precalc(){ | |
sch[0] = sch[1] = 1; | |
for(int i = 2; i < N; i++){ | |
sch[i] = ((6*i - 3) * sch[i-1] - (i - 2) * sch[i-2]) / (i + 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment