- UID
- 1821
- 精华
- 积分
- 3297
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
本帖最后由 Ayala 于 2016-7-28 23:39 编辑
用c写代码对我来说好痛苦!
- double sin(double x);
- double cos(double x);
- double acos(double x);
- double fmod(double x,double y);
- double fabs(double x);
- double floor(double x);
- double ceil(double x);
- typedef struct
- {
- double real;
- double img;
- }complex;
- #ifdef cp
- #undef cp
- typedef complex cp;
- #else
- typedef complex cp;
- #endif
- #ifndef max
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #endif
- #define N 1<<16
- __int64 a[N];
- __int64 b[N];
- __int64 c[N<<1];
- double cc[N<<1];
- double PI;
- const int LEN = 2, MOD = 100;
- const double MODf = 100.0;
- #ifdef x86_xp
- #ifndef _ftol2
- __int64 _ftol2(double x)
- {
- __int16 e,ee;
- __int64 r;
- __asm
- {
- fld qword ptr x
-
- fstsw word ptr [ee]
- mov ax,word ptr [ee]
- or ah,0xc
- mov word ptr [e],ax
-
- fldcw word ptr [e]
- fistp qword ptr [r]
- fldcw word ptr [ee]
-
- mov eax,dword ptr [r]
- mov edx,dword ptr [r+4]
- }
- }
- #endif
- #endif
- void add(complex a,complex b,complex *c)
- {
- //_fpreset();
- c->real =a.real + b.real;
- c->img =a.img + b.img;
- }
- void sub(complex a,complex b,complex *c)
- {
- //_fpreset();
- c->real=a.real-b.real;
- c->img=a.img-b.img;
- }
- void mul(complex a,complex b,complex *c)
- {
- //_fpreset();
- c->real =a.real*b.real - a.img*b.img;
- c->img =a.real*b.img + a.img*b.real;
- }
- void divi(complex a,complex b,complex *c)
- {
- //_fpreset();
- c->real =(a.real*b.real+a.img*b.img)/(b.real*b.real+b.img*b.img);
- c->img =(a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
- }
-
- void bit_reverse_copy(cp a[], int n, cp b[])
- {
- int i, j, k, u, m;
- for (u = 1, m = 0; u < n; u <<= 1, ++m);
- for (i = 0; i < n; ++i)
- {
- j = i; k = 0;
- for (u = 0; u < m; ++u, j >>= 1)
- k = (k << 1) | (j & 1);
- b[k] = a[i];
- //printf("b[%d]=%.2lf+%.2lfi\n",k,b[k].real,b[k].img);
- }
- }
- void FFT(cp _x[], int n, int flag)
- {
- static cp x[N << 1]={0.0};
- int i, j, k, kk, p, m;
- double alpha;
-
- cp wn,w,u,t,up,down;
- //_fpreset();
-
- bit_reverse_copy(_x, n, x);
-
- alpha = 2.0 * acos(-1.0);
- if (flag) alpha = -alpha;
-
- for (i = 1, m = 0; i < n; i <<= 1, ++m);
-
- for (i = 0, k = 2; i < m; ++i, k <<= 1)
- {
- wn.real = cos(alpha / k);
- wn.img = sin(alpha / k);
- for (j = 0; j < n; j += k)
- {
- w.real = 1.0;
- w.img = 0.0;
- kk = k >> 1;
- for (p = 0; p < kk; ++p)
- {
- mul(w, x[j + p + kk],&t);
- add(x[j + p],t,&up);
- sub(x[j + p],t,&down);
-
- x[j + p] = up;
- x[j + p + kk] = down;
-
- mul(w,wn,&u);
- w = u;
- }
- }
- }
- memcpy(_x, x, sizeof(cp) * n);
- }
- void polynomial_multiply(__int64 a[], int na, __int64 b[], int nb, __int64 c[], int *nc)
- {
- static cp x[N << 1], y[N << 1],u[N << 1];
- int i, n;
- //_fpreset();
- i = max(na, nb) << 1;
- for (n = 1; n < i; n <<= 1);
-
- for (i = 0; i < na; ++i)
- {
- x[i].real = a[i];
- x[i].img = 0;
- //printf("x[%i]=%.2lf+%.2lfi\n",i,x[i].real,x[i].img);
- }
-
- for (; i < n; ++i)
- {
- x[i].real = 0;
- x[i].img = 0;
- }
-
- FFT(x, n, 0);
-
-
- for (i = 0; i < nb; ++i)
- {
- y[i].real = b[i];
- y[i].img = 0;
- //printf("y[%i]=%.2lf+%.2lfi\n",i,y[i].real,y[i].img);
- }
-
- for (; i < n; ++i)
- {
- y[i].real = 0;
- y[i].img = 0;
- }
-
- FFT(y, n, 0);
-
- for (i = 0; i < n; ++i)
- {
- //printf("x[%i]=%.2lf+%.2lfi\n",i,x[i].real,x[i].img);
- //printf("y[%i]=%.2lf+%.2lfi\n",i,y[i].real,y[i].img);
- mul(x[i],y[i],&u[i]);
- //printf("u[%i]=%.2lf+%.2lfi\n",i,u[i].real,u[i].img);
- }
-
- FFT(u, n, 1);
-
- for (i = 0; i < n; ++i)
- {
- //printf("c[%d] .lf = %.53lf\n",i,u[i].real / n + 0.5);
- c[i] = _ftol2(u[i].real / n + 0.5);
- //printf("c[%d] I64d = %I64d\n",i,c[i]);
- }
- for (i = na + nb - 1; i > 1 && !c[i - 1]; --i);
- *nc = i;
- }
- void convert(char *s, __int64 a[], int * n)
- {
- int len = strlen(s), i, j, k,r;
- for (r = 0, i = len - LEN; i >= 0; i -= LEN)
- {
- for (j = k = 0; j < LEN; ++j)
- {
- k = k * 10 + (s[i + j] - '0');
- }
- a[r++] = k;
- //printf("@207 k = %d\n",k);
- }
- i += LEN;
- if (i)
- {
- for (j = k = 0; j < i; ++j)
- {
- k = k * 10 + (s[j] - '0');
- }
- a[r++] = k;
- //printf("@217 k = %d\n",k);
- }
- *n=r;
- //printf("@220 r = %d\n",r);
- }
-
- void print(__int64 a[], int n)
- {
- printf("%I64d", a[--n]);
- while (n) printf("%02I64d", a[--n]);
- puts("");
- }
- char buf[N + 10];
- #ifndef EOF
- #define EOF (-1)
- #endif
- #ifndef false
- #define false 0
- #endif
- #ifndef true
- #define true 1
- #endif
- /*
- void conc()
- {
-
- SetConsoleCP(936);
- SetConsoleOutputCP(936);
- SetConsoleMode(GetStdHandle(-10),0x7F);//STD_INPUT_HANDLE
- }
- */
- int mainCRTStartup()
- {
- int i,na, nb, nc,sign,e;
- __int64 t1, t2;
- double f1,f2;
-
- //conc();
- printf("******************************************************\n");
- printf("* *\n");
- printf("******************************************************\n");
- while (scanf("%s", buf) != EOF)
- {
- sign = false;
- if (buf[0] == '-')
- {
- sign = !sign;
- convert(buf + 1, a, &na);
- }
- else convert(buf, a, &na);
- scanf("%s", buf);
- if (buf[0] == '-')
- {
- sign = !sign;
- convert(buf + 1, b, &nb);
- }
- else convert(buf, b, &nb);
-
-
- polynomial_multiply(a, na, b, nb, c, &nc);
- t1 = 0;
- for (i = 0; i < nc; ++i)
- {
- t2 = t1 + c[i];
- c[i] = t2 % MOD;
- t1 = t2 / MOD;
- }
- for (; t1; t1 /= MOD) c[nc++] = t1 % MOD;
- if (sign) putchar('-');
- print(c, nc);
-
- printf("******************************************************\n");
- }
- return 0;
- }
-
复制代码
@echo off
:re
cls
echo /*********************************************/
echo / 构建命令 /
echo /*********************************************/
.\tools\x86\cl.exe .\src\hello_world.c /I".\inc\crt" /D"x86_xp" /I".\inc\api" /Fp:strict /Fa"Debug\hello_world.asm" /Fo"Debug\hello_world.obj" /c /MD
echo /*********************************************/
echo / /
echo /*********************************************/
.\tools\x86\link.exe .\Debug\hello_world.obj /LIBPATH:".\lib\wxp\i386" /LIBPATH:".\lib\Crt\i386" /OUT:"Debug\hello_world.exe" /NOLOGO /SUBSYSTEM:CONSOLE /MACHINE:X86 "kernel32.lib"
echo /*********************************************/
echo / /
echo /*********************************************/
pause
goto re
|
|