algorithm expressions - andstudy/forge GitHub Wiki
-
ํด๋ต ์์ด ๊ท์น ์ฐพ๊ธฐ ๋๋ฌด ๊ท์ฐฎ์. ใ
#include <stdio.h> #define NUMBERLENGTH 23 #define ONEDIGIT 10000 typedef struct _longlong { unsigned int h[NUMBERLENGTH]; } longlong; static int n, d; static longlong result; static longlong c[151][151]; static longlong e[151]; static longlong f[151][151]; void assign(longlong *a, unsigned int b) { int i; for(i=0 ; i<NUMBERLENGTH ; i++) { a->h[i] = b % ONEDIGIT; b /= ONEDIGIT; } } void add(longlong *c, const longlong *a, const longlong *b) { int i, carry; carry = 0; for(i=0 ; i<NUMBERLENGTH ; i++) { c->h[i] = a->h[i] + b->h[i] + carry; carry = c->h[i] / ONEDIGIT; c->h[i] %= ONEDIGIT; } } void sub(longlong *c, const longlong *a, const longlong *b) { int i, carry; carry = 0; for(i=0 ; i<NUMBERLENGTH ; i++) { if(a->h[i] >= b->h[i]) { c->h[i] = a->h[i] - b->h[i] + carry; carry = 0; } else { c->h[i] = (ONEDIGIT + a->h[i]) - b->h[i] + carry; carry = -1; } } } void mul(longlong *c, const longlong *a, const longlong *b) { int i, j, carry; unsigned int temp; int in, jn; longlong d; for(i=0 ; i<NUMBERLENGTH ; i++) d.h[i] = 0; in = jn = NUMBERLENGTH - 1; while(a->h[in] == 0) in--; in++; while(b->h[jn] == 0) jn--; jn++; for(i=0 ; i<in+1 ; i++) { carry = 0; for(j=0 ; j<jn+1 ; j++) { if(i+j < NUMBERLENGTH) { temp = (d.h[i+j] + a->h[i]*b->h[j] + carry) % ONEDIGIT; carry = (d.h[i+j] + a->h[i]*b->h[j] + carry) / ONEDIGIT; d.h[i+j] = temp; } } } for(i=0 ; i<NUMBERLENGTH ; i++) c->h[i] = d.h[i]; } void print(const longlong *a) { int sw; int i; sw = 0; for(i=NUMBERLENGTH-1 ; i>=0 ; i--) { if(!(sw==0 && a->h[i]==0)) { if(sw==0) { printf("%d", a->h[i]); sw = 1; } else { printf("%04d", a->h[i]); } } } if(sw==0) printf("0"); } void solve(void) { int i, j, k; longlong p; assign(&e[0], 1); assign(&e[1], 1); assign(&e[2], 2); for(i=3 ; i<=150 ; i++) { assign(&e[i], 0); for(j=1 ; j<=i ; j++) { mul(&p, &e[j-1], &e[i-j]); add(&e[i], &e[i], &p); } } for(i=0 ; i<=150 ; i++) { for(j=0 ; j<=150 ; j++) { if(i==0 && j==0) assign(&f[i][j], 1); else if(i<=j) f[i][j] = e[i]; else if(j==1) assign(&f[i][j], 1); else if(j==0) assign(&f[i][j], 0); else { assign(&f[i][j], 0); for(k=1 ; k<=i ; k++) { mul(&p, &f[k-1][j-1], &f[i-k][j]); add(&f[i][j], &f[i][j], &p); } } } } } void main(void) { solve(); while(scanf("%d %d", &n, &d) != EOF) { sub(&result, &f[n/2][d], &f[n/2][d-1]); print(&result); printf("\n"); } }
- X ๋ผ๋ ๋ฌธ์์ด ํํ์์ ๊ท์น์ ๋ง์กฑ ์ํค๋ ๊ฒฝ์ฐ์ ๊ฐ์๋ฅผ ์ฐพ๋ ๋ฌธ์ !
- ์ํ์ ๊ท๋ฉ๋ฒ์ ํ์ฉํด์ผ ํ๋ ๋ฌธ์ .. BUT..
- X ๋ฅผ ํํํ๋ 3๊ฐ์ง ๊ท์น์ ์ด์ฉํด ์กฐํฉ ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ฅผ ๋ชจ๋ ์ฐพ๋๋ค.
- () ๋ถํฐ ์์ํด์
- empty string ๊ณผ ๊ฒฐํฉ ์ํด
- ()์ ์์
- ํ์ฌ ๋ง๋ค์ด์ง X์ ์งํฉ ์์์ ๋ํด ์ค๋ณต์์ด ๊ตฌํจ
- () ๋ถํฐ ์์ํด์
- ์์ผ๋ก ๋จผ์ ํ์ด์ผ ํ๋๋ฐ.. ์ญ์๋ ์ฝ์ง ์์.
- ์๋ ์ฝ๋๋ ์ต์ ํ ํ๋ฉด ํด๋ณผ๋ง ํ์ง ์..??
- ์์ง์ ์ปดํฐ ์ฑ๋ฅ์ด ๋ณ๋ฃจ์ธ๋ฏ? ( ..)
- ํํ;;
using System;
using System.Collections.Generic;
using System.Text;
namespace Expressions
{
class X
{
public int depth;
public string value;
public X(int depth, string value)
{
this.depth = depth;
this.value = value;
}
// 0 Operation
public static X operator -(X x1)
{
return new X(x1.depth, x1.value);
}
// ๊ฒ์ท ์
ํ๊ธฐ
public static X operator +(X x1)
{
return new X(x1.depth+1, string.Format("({0})", x1.value));
}
// ๋๊ฐ ํฉํ๊ธฐ
public static X operator +(X x1, X x2)
{
return new X(Math.Max(x1.depth, x2.depth), string.Format("{0}{1}", x1.value, x2.value));
}
public override string ToString()
{
return string.Format("{0} : {1}", value, depth);
}
}
class Combination
{
List<X> pool;
Dictionary<string, int> dic;
public Combination()
{
pool = new List<X>();
dic = new Dictionary<string, int>();
}
public void Add(X x)
{
if (dic.ContainsKey(x.value))
return;
pool.Add(x);
dic.Add(x.value, 1);
}
public Combination Next()
{
Combination next = new Combination();
foreach (X x in pool)
{
next.Add(-x);
}
foreach (X x in pool)
{
next.Add(+x);
}
foreach (X x1 in pool)
{
foreach (X x2 in pool)
{
next.Add(x1 + x2);
}
}
return next;
}
public int Find(int length, int depth)
{
int result= 0;
foreach (X x in pool)
{
if (x.value.Length==length && x.depth==depth)
{
//Console.WriteLine("๋ต : {0}", x);
result++;
}
}
return result;
}
public void Display()
{
int order = 0;
foreach(X x in pool)
{
Console.WriteLine(string.Format("{0}\t{1}", order++, x));
}
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
// 1. > ์กฐํฉ ๋ฐ์ดํฐ ์์ง
Combination combination = new Combination();
combination.Add(new X(1, "()"));
for (int i = 0; i < 4/*1000*/; i++)
{
// ํ์ฌ ํ๋์จ์ด๋ก๋ 4๊น์ง๋ง ํด๋ณผ๋ง ํจ
combination = combination.Next();
combination.Display();
}
// 2. > ๋ฌธ์ ํ๊ธฐ
Console.WriteLine(combination.Find(6, 2));
Console.WriteLine(combination.Find(300, 150));
}
}
}
-
ํ ์คํธ 1์ฐจ
#include <stdio.h> typedef unsigned long long BigInteger; BigInteger Matrix[151][151]; BigInteger GetMatrix(int n, int d ) { if( n < 0 || d < 0 ) return 0; if( n < d ) d = n; return Matrix[n][d]; } void CalMatrix(int n, int d) { for(int j = 0; j < n; j++ ) { Matrix[n][d] += GetMatrix(j,d-1)*GetMatrix(n-1-j,d); } } void MakeMatrix() { Matrix[0][0] = 1; for(int i = 1; i <= 150; i++ ) { for(int j = 1; j <= i; j++ ) { CalMatrix(i, j); } } } void PrintResult(int n, int d) { printf("%llu\n", Matrix[n][d] - Matrix[n][d-1]); } int main() { int n, d; MakeMatrix(); while( scanf("%d %d", &n, &d) == 2 ) { //n /= 2; PrintResult(n, d); } return 0; }
-
Solved ๋ฐ์ ์ฝ๋ ์ ๋๋ค. charํ๊ฐ์ 1์๋ฆฌ ํ๋๊ฒ๋ณด๋ค intํ ํ๊ฐ์ 4์๋ฆฌ์ฉ ํ๋๊ฒ ์๋๊ฐ 10๋ฐฐ์ ๋ ๋น ๋ฆ ๋๋ค.(์ ํํ ์ฐ๊ฑด ์๋๊ณ ๋๋์,,) ์ฃผ์์ฒ๋ฆฌ ํ ๊ฒ์ ๋ฐ๊พธ์ด์ฃผ๋ฉด ์๋๋น๊ต๋ฅผ ํ์ค ์ ์์ต๋๋ค.
- charํ์ผ๋ก ๊ณ์ฐํ ๋ Show๋ฉ์๋๋ฅผ ์ฝ๊ฐ ์์ ํด์ผ ๊ฒ ๋ค์ ^^;; %04d๋ฅผ %d๋ก;
-
www.programming-challenges.com ์ ์ ์ถํ๋ฉด ์คํ์๊ฐ์ด 7์ด์ด์, UVa์ ์ ์ถํ๋ฉด 1์ด๋ค์.
- ๋๋ผ์ด๊ฑด ์ต๊ณ ์๋๊ฐ PC์์ 0.019์ด ๋ผ๋.... ํ
์คํธ ์
๋ ฅ๊ฐ์ ๋นผ๋ด์์ ๊ฒฐ๊ณผ๋ง ๋ฟ๋ ค์ค๊ฑฐ๋ผ๊ณ ์ถ์ธกํด๋ด
๋๋ค;
- ์์ผ ํ๋ฐํด์ ์ ๋ ฅ๊ฐ ๋ฐ์ ๋, ๋ด IP๋ก ์๊ฒ ํ๋ฉด ๋๊ฒ ๋ ๊ฑธ์! ํด๋ด์ผ์ง ใ ใ
- ๋ง์์! ใ ํ์ง๋ง... ๊ทธ๋ฐ์์ผ๋ก ์ ๋ ฅ๊ฐ ๋นผ์์ ๊ฒฐ๊ณผ๋ง ์ถ๋ ฅํ๊ฒ 3n+1์ ๋ง๋ค์ด์ ์ ์ถํ์๋๋ฐ.. ์ต๊ณ ์๋๋ณด๋ค ๋๋ ธ์๋ค๋ ;;;;;
- ์ค.. ์ข์ ์๊ฐ์ธ๋ฐ์.. ใ ใ
- ๋๋ผ์ด๊ฑด ์ต๊ณ ์๋๊ฐ PC์์ 0.019์ด ๋ผ๋.... ํ
์คํธ ์
๋ ฅ๊ฐ์ ๋นผ๋ด์์ ๊ฒฐ๊ณผ๋ง ๋ฟ๋ ค์ค๊ฑฐ๋ผ๊ณ ์ถ์ธกํด๋ด
๋๋ค;
-
๊ตฌํํ ๋ ๋ณด๋, ์ฝ๋ฉ์์ฐํ ๋๋ ๊ณฑํ๊ธฐ๋ฅผ ์์ ์๋ชป ๊ตฌํํ์์ด์, :$
-
์ฑ ์ ํด๋ต๋ณด๋ค๋ ์๋ ๋น ๋ฅธ๊ฑฐ ๊ฐ์์!
- ์ค~ ๋์ด์ค~ ^ใ ^/
-
์ญ์ ํผ๊ณคํ ๋ ์ ์ ์์ฃผ๋๊ฒ ์ต๊ณ , ๋๋ถ์ KLDP๋ชป๊ฐ๋ค๋;;;
#include <stdio.h>
#include <string.h>
#define max(x,y) ((x)>(y)?(x):(y))
#define MAXLEN 25
#define ONEDIGIT 10000
typedef int num;
/*
#define MAXLEN 200
#define ONEDIGIT 10
typedef char num;
*/
class BigNumber
{
public:
BigNumber()
{
Init();
}
BigNumber(char* in)
{
Init();
length = strlen(in);
for(int i = length-1; i >= 0; i-- )
{
Number[i] = in[length-i-1] - '0';
}
}
BigNumber(int in)
{
Init();
while(in)
{
Number[length] = in%ONEDIGIT;
in/= ONEDIGIT;
length++;
}
}
const BigNumber& operator+=(const BigNumber& rhs)
{
int i;
int carry = 0;
int MaxLen = max(rhs.length, length);
for( i = 0; i < MaxLen; i++ )
{
Number[i] = rhs.Number[i] + Number[i] + carry;
if( Number[i] >= ONEDIGIT )
{
Number[i] -= ONEDIGIT;
carry = 1;
}
else
{
carry = 0;
}
}
if( carry )
{
length = MaxLen+1;
Number[i] = carry;
}
else
{
length = MaxLen;
}
return *this;
}
const BigNumber& operator-=(const BigNumber& rhs)
{
for(int i = 0; i < length; i++ )
{
Number[i] = Number[i] - rhs.Number[i];
while( Number[i] < 0 )
{
Number[i] += ONEDIGIT;
Number[i+1] -= 1;
}
}
int i;
for(i = length-1; i >= 0; i-- )
{
if( Number[i] != 0 )
{
break;
}
}
length = i+1;
return *this;
}
const BigNumber& operator*=(const BigNumber& rhs)
{
BigNumber Temp;
int i, j;
int carry = 0;
for( i = 0; i < length; i++ )
{
for( j = 0; j < rhs.length; j++ )
{
Temp.Number[i+j] += Number[i]*rhs.Number[j];
if(Temp.Number[i+j] >= ONEDIGIT )
{
Temp.Number[i+j+1] += Temp.Number[i+j]/ONEDIGIT;
Temp.Number[i+j] %= ONEDIGIT;
}
}
}
for( i = MAXLEN-1; i >= 0; i-- )
{
if(Temp.Number[i] != 0 )
{
Temp.length = i+1;
break;
}
}
*this = Temp;
return *this;
}
void Show()
{
if( length == 0 )
{
printf("0\n");
return;
}
printf("%d", Number[length-1]);
for(int i = length-2; i >= 0; i-- )
{
printf("%04d", Number[i]);
}
printf("\n");
}
private:
void Init()
{
length = 0;
for(int i = 0; i < MAXLEN; i++ )
{
Number[i] = 0;
}
}
num Number[MAXLEN];
int length;
};
const BigNumber operator+(const BigNumber& lhs, const BigNumber& rhs)
{
BigNumber Temp(lhs);
Temp += rhs;
return Temp;
}
const BigNumber operator-(const BigNumber& lhs, const BigNumber& rhs)
{
BigNumber Temp(lhs);
Temp -= rhs;
return Temp;
}
const BigNumber operator*(const BigNumber& lhs, const BigNumber& rhs)
{
BigNumber Temp(lhs);
Temp *= rhs;
return Temp;
}
BigNumber Matrix[151][151] = {0};
BigNumber GetMatrix(int n, int d )
{
if( n == 0 && d == 0 ) return 1;
if( n < d ) d = n;
return Matrix[n][d];
}
void PrintResult(int n, int d )
{
BigNumber Temp(GetMatrix(n, d) - GetMatrix(n, d-1));
Temp.Show();
}
void CalMatrix(int n, int d )
{
for(int i = 0; i < n; i++ )
{
Matrix[n][d] += GetMatrix(i, d-1)*GetMatrix(n-1-i, d);
}
}
void MakeMatrix()
{
Matrix[0][0] = 1;
for(int i = 1; i <= 150; i++ )
{
for(int j = 1; j <= i; j++ )
{
CalMatrix(i, j);
}
}
}
int main()
{
int n, d;
MakeMatrix();
while( scanf("%d %d", &n, &d) == 2 )
{
n /= 2;
PrintResult(n, d);
}
return 0;
}