题解 P3169 【[CQOI2015]多项式 】

劉子颺

2018-03-24 16:53:33

Solution

毒瘤题 考察的是高精度。 第一Ai的递推式很简单 矩阵乘法就行 那么发现n-m比较小 利用二项式定理就可以了 ``` #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> #include <iostream> #include <cassert> #include <sstream> #include <numeric> #include <climits> #include <string> #include <cctype> #include <ctime> #include <iomanip> #include <cmath> #include <vector> #include <queue> #include <list> #include <map> #include <set> using namespace std; // -*- C++ -*- forwarding header. // Copyright (C) 1997-2014 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Under Section 7 of GPL version 3, you are granted additional // permissions described in the GCC Runtime Library Exception, version // 3.1, as published by the Free Software Foundation. // You should have received a copy of the GNU General Public License and // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // <http://www.gnu.org/licenses/>. /** @file include/cstdio * This is a Standard C++ Library file. You should @c \#include this file * in your programs, rather than any of the @a *.h implementation files. * * This is the C++ version of the Standard C Library header @c stdio.h, * and its contents are (mostly) the same as that header, but are all * contained in the namespace @c std (except for names which are defined * as macros in C). */ // // ISO C++ 14882: 27.8.2 C Library files // #pragma GCC system_header #include <bits/c++config.h> #include <stdio.h> #ifndef _GLIBCXX_CSTDIO #define _GLIBCXX_CSTDIO 1 #ifndef _GLIBCXX_HAVE_GETS extern "C" char* gets (char* __s) __attribute__((deprecated)); #endif // Get rid of those macros defined in <stdio.h> in lieu of real functions. #undef clearerr #undef fclose #undef feof #undef ferror #undef fflush #undef fgetc #undef fgetpos #undef fgets #undef fopen #undef fprintf #undef fputc #undef fputs #undef fread #undef freopen #undef fscanf #undef fseek #undef fsetpos #undef ftell #undef fwrite #undef getc #undef getchar #if __cplusplus <= 201103L # undef gets #endif #undef perror #undef printf #undef putc #undef putchar #undef puts #undef remove #undef rename #undef rewind #undef scanf #undef setbuf #undef setvbuf #undef sprintf #undef sscanf #undef tmpfile #undef tmpnam #undef ungetc #undef vfprintf #undef vprintf #undef vsprintf namespace std { using ::FILE; using ::fpos_t; using ::clearerr; using ::fclose; using ::feof; using ::ferror; using ::fflush; using ::fgetc; using ::fgetpos; using ::fgets; using ::fopen; using ::fprintf; using ::fputc; using ::fputs; using ::fread; using ::freopen; using ::fscanf; using ::fseek; using ::fsetpos; using ::ftell; using ::fwrite; using ::getc; using ::getchar; #if __cplusplus <= 201103L // LWG 2249 using ::gets; #endif using ::perror; using ::printf; using ::putc; using ::putchar; using ::puts; using ::remove; using ::rename; using ::rewind; using ::scanf; using ::setbuf; using ::setvbuf; using ::sprintf; using ::sscanf; using ::tmpfile; #if _GLIBCXX_USE_TMPNAM using ::tmpnam; #endif using ::ungetc; using ::vfprintf; using ::vprintf; using ::vsprintf; } // namespace #if _GLIBCXX_USE_C99 #undef snprintf #undef vfscanf #undef vscanf #undef vsnprintf #undef vsscanf namespace __gnu_cxx { #if _GLIBCXX_USE_C99_CHECK || _GLIBCXX_USE_C99_DYNAMIC extern "C" int (snprintf)(char * __restrict, std::size_t, const char * __restrict, ...) throw (); extern "C" int (vfscanf)(FILE * __restrict, const char * __restrict, __gnuc_va_list); extern "C" int (vscanf)(const char * __restrict, __gnuc_va_list); extern "C" int (vsnprintf)(char * __restrict, std::size_t, const char * __restrict, __gnuc_va_list) throw (); extern "C" int (vsscanf)(const char * __restrict, const char * __restrict, __gnuc_va_list) throw (); #endif #if !_GLIBCXX_USE_C99_DYNAMIC using ::snprintf; using ::vfscanf; using ::vscanf; using ::vsnprintf; using ::vsscanf; #endif } // namespace __gnu_cxx namespace std { using ::__gnu_cxx::snprintf; using ::__gnu_cxx::vfscanf; using ::__gnu_cxx::vscanf; using ::__gnu_cxx::vsnprintf; using ::__gnu_cxx::vsscanf; } // namespace std #endif // _GLIBCXX_USE_C99 #endif namespace fastIO{ #define BUF_SIZE 100000 #define OUT_SIZE 100000 #define ll long long //fread->read bool IOerror=0; inline char nc(){ static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; if (p1==pend){ p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); if (pend==p1){IOerror=1;return -1;} //{printf("IO error!\n");system("pause");for (;;);exit(0);} } return *p1++; } inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} inline void read(int &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror)return; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (sign)x=-x; } inline void read(ll &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror)return; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (sign)x=-x; } inline void read(double &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror)return; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (ch=='.'){ double tmp=1; ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0'); } if (sign)x=-x; } inline void read(char *s){ char ch=nc(); for (;blank(ch);ch=nc()); if (IOerror)return; for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch; *s=0; } inline void read(char &c){ for (c=nc();blank(c);c=nc()); if (IOerror){c=-1;return;} } //getchar->read inline void read1(int &x){ char ch;int bo=0;x=0; for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1; for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar()); if (bo)x=-x; } inline void read1(ll &x){ char ch;int bo=0;x=0; for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1; for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar()); if (bo)x=-x; } inline void read1(double &x){ char ch;int bo=0;x=0; for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1; for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar()); if (ch=='.'){ double tmp=1; for (ch=getchar();ch>='0'&&ch<='9';tmp/=10.0,x+=tmp*(ch-'0'),ch=getchar()); } if (bo)x=-x; } inline void read1(char *s){ char ch=getchar(); for (;blank(ch);ch=getchar()); for (;!blank(ch);ch=getchar())*s++=ch; *s=0; } inline void read1(char &c){for (c=getchar();blank(c);c=getchar());} //scanf->read inline void read2(int &x){scanf("%d",&x);} inline void read2(ll &x){ #ifdef _WIN32 scanf("%I64d",&x); #else #ifdef __linux scanf("%lld",&x); #else puts("error:can't recognize the system!"); #endif #endif } inline void read2(double &x){scanf("%lf",&x);} inline void read2(char *s){scanf("%s",s);} inline void read2(char &c){scanf(" %c",&c);} inline void readln2(char *s){gets(s);} //fwrite->write struct Ostream_fwrite{ char *buf,*p1,*pend; Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;} void out(char ch){ if (p1==pend){ fwrite(buf,1,BUF_SIZE,stdout);p1=buf; } *p1++=ch; } void print(int x){ static char s[15],*s1;s1=s; if (!x)*s1++='0';if (x<0)out('-'),x=-x; while(x)*s1++=x%10+'0',x/=10; while(s1--!=s)out(*s1); out(' '); } void println(int x){ static char s[15],*s1;s1=s; if (!x)*s1++='0';if (x<0)out('-'),x=-x; while(x)*s1++=x%10+'0',x/=10; while(s1--!=s)out(*s1); out('\n'); } void print(ll x){ static char s[25],*s1;s1=s; if (!x)*s1++='0';if (x<0)out('-'),x=-x; while(x)*s1++=x%10+'0',x/=10; while(s1--!=s)out(*s1); } void println(ll x){ static char s[25],*s1;s1=s; if (!x)*s1++='0';if (x<0)out('-'),x=-x; while(x)*s1++=x%10+'0',x/=10; while(s1--!=s)out(*s1); out('\n'); } void print(double x,int y){ static ll mul[]={1,10,100,1000,10000,100000,1000000,10000000,100000000, 1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL, 100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL}; if (x<-1e-12)out('-'),x=-x;x*=mul[y]; ll x1=(ll)floor(x); if (x-floor(x)>=0.5)++x1; ll x2=x1/mul[y],x3=x1-x2*mul[y]; print(x2); if (y>0){out('.'); for (size_t i=1;i<y&&x3*mul[i]<mul[y];out('0'),++i); print(x3);} } void println(double x,int y){print(x,y);out('\n');} void print(char *s){while (*s)out(*s++);} void println(char *s){while (*s)out(*s++);out('\n');} void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}} ~Ostream_fwrite(){flush();} }Ostream; inline void print(int x){Ostream.print(x);} inline void println(int x){Ostream.println(x);} inline void print(char x){Ostream.out(x);} inline void println(char x){Ostream.out(x);Ostream.out('\n');} inline void print(ll x){Ostream.print(x);} inline void println(ll x){Ostream.println(x);} inline void print(double x,int y){Ostream.print(x,y);} inline void println(double x,int y){Ostream.println(x,y);} inline void print(char *s){Ostream.print(s);} inline void println(char *s){Ostream.println(s);} inline void println(){Ostream.out('\n');} inline void flush(){Ostream.flush();} //puts->write char Out[OUT_SIZE],*o=Out; inline void print1(int x){ static char buf[15]; char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x; while(x)*p1++=x%10+'0',x/=10; while(p1--!=buf)*o++=*p1; } inline void println1(int x){print1(x);*o++='\n';} inline void print1(ll x){ static char buf[25]; char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x; while(x)*p1++=x%10+'0',x/=10; while(p1--!=buf)*o++=*p1; } inline void println1(ll x){print1(x);*o++='\n';} inline void print1(char c){*o++=c;} inline void println1(char c){*o++=c;*o++='\n';} inline void print1(char *s){while (*s)*o++=*s++;} inline void println1(char *s){print1(s);*o++='\n';} inline void println1(){*o++='\n';} inline void flush1(){if (o!=Out){if (*(o-1)=='\n')*--o=0;puts(Out);}} struct puts_write{ ~puts_write(){flush1();} }_puts; inline void print2(int x){printf("%d",x);} inline void println2(int x){printf("%d\n",x);} inline void print2(char x){printf("%c",x);} inline void println2(char x){printf("%c\n",x);} inline void print2(ll x){ #ifdef _WIN32 printf("%I64d",x); #else #ifdef __linux printf("%lld",x); #else puts("error:can't recognize the system!"); #endif #endif } inline void println2(ll x){print2(x);printf("\n");} inline void println2(){printf("\n");} #undef ll #undef OUT_SIZE #undef BUF_SIZE }; #define LL long long #define DIGIT 4 //四位隔开,即万进制 #define DEPTH 10000 //万进制 #define MAX 10000 typedef int bignum_t[MAX+1]; /************************************************************************/ /* 读取操作数,对操作数进行处理存储在数组里 */ /************************************************************************/ int read(bignum_t a,istream&is=cin) { char buf[MAX*DIGIT+1],ch ; int i,j ; memset((void*)a,0,sizeof(bignum_t)); if(!(is>>buf))return 0 ; for(a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--) ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ; for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0'); for(i=1;i<=a[0];i++) for(a[i]=0,j=0;j<DIGIT;j++) a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0' ; for(;!a[a[0]]&&a[0]>1;a[0]--); return 1 ; } void write(const bignum_t a,ostream&os=cout) { int i,j ; for(os<<a[i=a[0]],i--;i;i--) for(j=DEPTH/10;j;j/=10) os<<a[i]/j%10 ; } int comp(const bignum_t a,const bignum_t b) { int i ; if(a[0]!=b[0]) return a[0]-b[0]; for(i=a[0];i;i--) if(a[i]!=b[i]) return a[i]-b[i]; return 0 ; } int comp(const bignum_t a,const int b) { int c[12]={1}; for(c[1]=b;c[c[0]]>=DEPTH;c[c[0]+1]=c[c[0]]/DEPTH,c[c[0]]%=DEPTH,c[0]++); return comp(a,c); } int comp(const bignum_t a,const int c,const int d,const bignum_t b) { int i,t=0,O=-DEPTH*2 ; if(b[0]-a[0]<d&&c) return 1 ; for(i=b[0];i>d;i--) { t=t*DEPTH+a[i-d]*c-b[i]; if(t>0)return 1 ; if(t<O)return 0 ; } for(i=d;i;i--) { t=t*DEPTH-b[i]; if(t>0)return 1 ; if(t<O)return 0 ; } return t>0 ; } /************************************************************************/ /* 大数与大数相加 */ /************************************************************************/ void add(bignum_t a,const bignum_t b) { int i ; for(i=1;i<=b[0];i++) if((a[i]+=b[i])>=DEPTH) a[i]-=DEPTH,a[i+1]++; if(b[0]>=a[0]) a[0]=b[0]; else for(;a[i]>=DEPTH&&i<a[0];a[i]-=DEPTH,i++,a[i]++); a[0]+=(a[a[0]+1]>0); } /************************************************************************/ /* 大数与小数相加 */ /************************************************************************/ void add(bignum_t a,const int b) { int i=1 ; for(a[1]+=b;a[i]>=DEPTH&&i<a[0];a[i+1]+=a[i]/DEPTH,a[i]%=DEPTH,i++); for(;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++); } /************************************************************************/ /* 大数相减(被减数>=减数) */ /************************************************************************/ void sub(bignum_t a,const bignum_t b) { int i ; for(i=1;i<=b[0];i++) if((a[i]-=b[i])<0) a[i+1]--,a[i]+=DEPTH ; for(;a[i]<0;a[i]+=DEPTH,i++,a[i]--); for(;!a[a[0]]&&a[0]>1;a[0]--); } /************************************************************************/ /* 大数减去小数(被减数>=减数) */ /************************************************************************/ void sub(bignum_t a,const int b) { int i=1 ; for(a[1]-=b;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++); for(;!a[a[0]]&&a[0]>1;a[0]--); } void sub(bignum_t a,const bignum_t b,const int c,const int d) { int i,O=b[0]+d ; for(i=1+d;i<=O;i++) if((a[i]-=b[i-d]*c)<0) a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH ; for(;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++); for(;!a[a[0]]&&a[0]>1;a[0]--); } /************************************************************************/ /* 大数相乘,读入被乘数a,乘数b,结果保存在c[] */ /************************************************************************/ void mul(bignum_t c,const bignum_t a,const bignum_t b) { int i,j ; memset((void*)c,0,sizeof(bignum_t)); for(c[0]=a[0]+b[0]-1,i=1;i<=a[0];i++) for(j=1;j<=b[0];j++) if((c[i+j-1]+=a[i]*b[j])>=DEPTH) c[i+j]+=c[i+j-1]/DEPTH,c[i+j-1]%=DEPTH ; for(c[0]+=(c[c[0]+1]>0);!c[c[0]]&&c[0]>1;c[0]--); } /************************************************************************/ /* 大数乘以小数,读入被乘数a,乘数b,结果保存在被乘数 */ /************************************************************************/ void mul(bignum_t a,const int b) { int i ; for(a[1]*=b,i=2;i<=a[0];i++) { a[i]*=b ; if(a[i-1]>=DEPTH) a[i]+=a[i-1]/DEPTH,a[i-1]%=DEPTH ; } for(;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++); for(;!a[a[0]]&&a[0]>1;a[0]--); } void mul(bignum_t b,const bignum_t a,const int c,const int d) { int i ; memset((void*)b,0,sizeof(bignum_t)); for(b[0]=a[0]+d,i=d+1;i<=b[0];i++) if((b[i]+=a[i-d]*c)>=DEPTH) b[i+1]+=b[i]/DEPTH,b[i]%=DEPTH ; for(;b[b[0]+1];b[0]++,b[b[0]+1]=b[b[0]]/DEPTH,b[b[0]]%=DEPTH); for(;!b[b[0]]&&b[0]>1;b[0]--); } /**************************************************************************/ /* 大数相除,读入被除数a,除数b,结果保存在c[]数组 */ /* 需要comp()函数 */ /**************************************************************************/ void div(bignum_t c,bignum_t a,const bignum_t b) { int h,l,m,i ; memset((void*)c,0,sizeof(bignum_t)); c[0]=(b[0]<a[0]+1)?(a[0]-b[0]+2):1 ; for(i=c[0];i;sub(a,b,c[i]=m,i-1),i--) for(h=DEPTH-1,l=0,m=(h+l+1)>>1;h>l;m=(h+l+1)>>1) if(comp(b,m,i-1,a))h=m-1 ; else l=m ; for(;!c[c[0]]&&c[0]>1;c[0]--); c[0]=c[0]>1?c[0]:1 ; } void div(bignum_t a,const int b,int&c) { int i ; for(c=0,i=a[0];i;c=c*DEPTH+a[i],a[i]=c/b,c%=b,i--); for(;!a[a[0]]&&a[0]>1;a[0]--); } /************************************************************************/ /* 大数平方根,读入大数a,结果保存在b[]数组里 */ /* 需要comp()函数 */ /************************************************************************/ void sqrt(bignum_t b,bignum_t a) { int h,l,m,i ; memset((void*)b,0,sizeof(bignum_t)); for(i=b[0]=(a[0]+1)>>1;i;sub(a,b,m,i-1),b[i]+=m,i--) for(h=DEPTH-1,l=0,b[i]=m=(h+l+1)>>1;h>l;b[i]=m=(h+l+1)>>1) if(comp(b,m,i-1,a))h=m-1 ; else l=m ; for(;!b[b[0]]&&b[0]>1;b[0]--); for(i=1;i<=b[0];b[i++]>>=1); } /************************************************************************/ /* 返回大数的长度 */ /************************************************************************/ int length(const bignum_t a) { int t,ret ; for(ret=(a[0]-1)*DIGIT,t=a[a[0]];t;t/=10,ret++); return ret>0?ret:1 ; } /************************************************************************/ /* 返回指定位置的数字,从低位开始数到第b位,返回b位上的数 */ /************************************************************************/ int digit(const bignum_t a,const int b) { int i,ret ; for(ret=a[(b-1)/DIGIT+1],i=(b-1)%DIGIT;i;ret/=10,i--); return ret%10 ; } /************************************************************************/ /* 返回大数末尾0的个数 */ /************************************************************************/ int zeronum(const bignum_t a) { int ret,t ; for(ret=0;!a[ret+1];ret++); for(t=a[ret+1],ret*=DIGIT;!(t%10);t/=10,ret++); return ret ; } void comp(int*a,const int l,const int h,const int d) { int i,j,t ; for(i=l;i<=h;i++) for(t=i,j=2;t>1;j++) while(!(t%j)) a[j]+=d,t/=j ; } void convert(int*a,const int h,bignum_t b) { int i,j,t=1 ; memset(b,0,sizeof(bignum_t)); for(b[0]=b[1]=1,i=2;i<=h;i++) if(a[i]) for(j=a[i];j;t*=i,j--) if(t*i>DEPTH) mul(b,t),t=1 ; mul(b,t); } #define SGN(x) ((x)>0?1:((x)<0?-1:0)) #define ABS(x) ((x)>0?(x):-(x)) int read(bignum_t a,int&sgn,istream&is=cin) { char str[MAX*DIGIT+2],ch,*buf ; int i,j ; memset((void*)a,0,sizeof(bignum_t)); if(!(is>>str))return 0 ; buf=str,sgn=1 ; if(*buf=='-')sgn=-1,buf++; for(a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--) ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ; for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0'); for(i=1;i<=a[0];i++) for(a[i]=0,j=0;j<DIGIT;j++) a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0' ; for(;!a[a[0]]&&a[0]>1;a[0]--); if(a[0]==1&&!a[1])sgn=0 ; return 1 ; } struct bignum { bignum_t num ; int sgn ; public : inline bignum() { memset(num,0,sizeof(bignum_t)); num[0]=1 ; sgn=0 ; } inline int operator!() { return num[0]==1&&!num[1]; } inline bignum&operator=(const bignum&a) { memcpy(num,a.num,sizeof(bignum_t)); sgn=a.sgn ; return*this ; } inline bignum&operator=(const int a) { memset(num,0,sizeof(bignum_t)); num[0]=1 ; sgn=SGN (a); add(num,sgn*a); return*this ; } ; inline bignum&operator+=(const bignum&a) { if(sgn==a.sgn)add(num,a.num); else if (sgn&&a.sgn) { int ret=comp(num,a.num); if(ret>0)sub(num,a.num); else if(ret<0) { bignum_t t ; memcpy(t,num,sizeof(bignum_t)); memcpy(num,a.num,sizeof(bignum_t)); sub (num,t); sgn=a.sgn ; } else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ; } else if(!sgn) memcpy(num,a.num,sizeof(bignum_t)),sgn=a.sgn ; return*this ; } inline bignum&operator+=(const int a) { if(sgn*a>0)add(num,ABS(a)); else if(sgn&&a) { int ret=comp(num,ABS(a)); if(ret>0)sub(num,ABS(a)); else if(ret<0) { bignum_t t ; memcpy(t,num,sizeof(bignum_t)); memset(num,0,sizeof(bignum_t)); num[0]=1 ; add(num,ABS (a)); sgn=-sgn ; sub(num,t); } else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ; } else if (!sgn)sgn=SGN(a),add(num,ABS(a)); return*this ; } inline bignum operator+(const bignum&a) { bignum ret ; memcpy(ret.num,num,sizeof (bignum_t)); ret.sgn=sgn ; ret+=a ; return ret ; } inline bignum operator+(const int a) { bignum ret ; memcpy(ret.num,num,sizeof (bignum_t)); ret.sgn=sgn ; ret+=a ; return ret ; } inline bignum&operator-=(const bignum&a) { if(sgn*a.sgn<0)add(num,a.num); else if (sgn&&a.sgn) { int ret=comp(num,a.num); if(ret>0)sub(num,a.num); else if(ret<0) { bignum_t t ; memcpy(t,num,sizeof(bignum_t)); memcpy(num,a.num,sizeof(bignum_t)); sub(num,t); sgn=-sgn ; } else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ; } else if(!sgn)add (num,a.num),sgn=-a.sgn ; return*this ; } inline bignum&operator-=(const int a) { if(sgn*a<0)add(num,ABS(a)); else if(sgn&&a) { int ret=comp(num,ABS(a)); if(ret>0)sub(num,ABS(a)); else if(ret<0) { bignum_t t ; memcpy(t,num,sizeof(bignum_t)); memset(num,0,sizeof(bignum_t)); num[0]=1 ; add(num,ABS(a)); sub(num,t); sgn=-sgn ; } else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ; } else if (!sgn)sgn=-SGN(a),add(num,ABS(a)); return*this ; } inline bignum operator-(const bignum&a) { bignum ret ; memcpy(ret.num,num,sizeof(bignum_t)); ret.sgn=sgn ; ret-=a ; return ret ; } inline bignum operator-(const int a) { bignum ret ; memcpy(ret.num,num,sizeof(bignum_t)); ret.sgn=sgn ; ret-=a ; return ret ; } inline bignum&operator*=(const bignum&a) { bignum_t t ; mul(t,num,a.num); memcpy(num,t,sizeof(bignum_t)); sgn*=a.sgn ; return*this ; } inline bignum&operator*=(const int a) { mul(num,ABS(a)); sgn*=SGN(a); return*this ; } inline bignum operator*(const bignum&a) { bignum ret ; mul(ret.num,num,a.num); ret.sgn=sgn*a.sgn ; return ret ; } inline bignum operator*(const int a) { bignum ret ; memcpy(ret.num,num,sizeof (bignum_t)); mul(ret.num,ABS(a)); ret.sgn=sgn*SGN(a); return ret ; } inline bignum&operator/=(const bignum&a) { bignum_t t ; div(t,num,a.num); memcpy (num,t,sizeof(bignum_t)); sgn=(num[0]==1&&!num[1])?0:sgn*a.sgn ; return*this ; } inline bignum&operator/=(const int a) { int t ; div(num,ABS(a),t); sgn=(num[0]==1&&!num [1])?0:sgn*SGN(a); return*this ; } inline bignum operator/(const bignum&a) { bignum ret ; bignum_t t ; memcpy(t,num,sizeof(bignum_t)); div(ret.num,t,a.num); ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*a.sgn ; return ret ; } inline bignum operator/(const int a) { bignum ret ; int t ; memcpy(ret.num,num,sizeof(bignum_t)); div(ret.num,ABS(a),t); ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*SGN(a); return ret ; } inline bignum&operator%=(const bignum&a) { bignum_t t ; div(t,num,a.num); if(num[0]==1&&!num[1])sgn=0 ; return*this ; } inline int operator%=(const int a) { int t ; div(num,ABS(a),t); memset(num,0,sizeof (bignum_t)); num[0]=1 ; add(num,t); return t ; } inline bignum operator%(const bignum&a) { bignum ret ; bignum_t t ; memcpy(ret.num,num,sizeof(bignum_t)); div(t,ret.num,a.num); ret.sgn=(ret.num[0]==1&&!ret.num [1])?0:sgn ; return ret ; } inline int operator%(const int a) { bignum ret ; int t ; memcpy(ret.num,num,sizeof(bignum_t)); div(ret.num,ABS(a),t); memset(ret.num,0,sizeof(bignum_t)); ret.num[0]=1 ; add(ret.num,t); return t ; } inline bignum&operator++() { *this+=1 ; return*this ; } inline bignum&operator--() { *this-=1 ; return*this ; } ; inline int operator>(const bignum&a) { return sgn>0?(a.sgn>0?comp(num,a.num)>0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<0:0):a.sgn<0); } ; inline int operator>(const int a) { return sgn>0?(a>0?comp(num,a)>0:1):(sgn<0?(a<0?comp(num,-a)<0:0):a<0); } ; inline int operator>=(const bignum&a) { return sgn>0?(a.sgn>0?comp(num,a.num)>=0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<=0:0):a.sgn<=0); } ; inline int operator>=(const int a) { return sgn>0?(a>0?comp(num,a)>=0:1):(sgn<0?(a<0?comp(num,-a)<=0:0):a<=0); } ; inline int operator<(const bignum&a) { return sgn<0?(a.sgn<0?comp(num,a.num)>0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<0:0):a.sgn>0); } ; inline int operator<(const int a) { return sgn<0?(a<0?comp(num,-a)>0:1):(sgn>0?(a>0?comp(num,a)<0:0):a>0); } ; inline int operator<=(const bignum&a) { return sgn<0?(a.sgn<0?comp(num,a.num)>=0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<=0:0):a.sgn>=0); } ; inline int operator<=(const int a) { return sgn<0?(a<0?comp(num,-a)>=0:1): (sgn>0?(a>0?comp(num,a)<=0:0):a>=0); } ; inline int operator==(const bignum&a) { return(sgn==a.sgn)?!comp(num,a.num):0 ; } ; inline int operator==(const int a) { return(sgn*a>=0)?!comp(num,ABS(a)):0 ; } ; inline int operator!=(const bignum&a) { return(sgn==a.sgn)?comp(num,a.num):1 ; } ; inline int operator!=(const int a) { return(sgn*a>=0)?comp(num,ABS(a)):1 ; } ; inline int operator[](const int a) { return digit(num,a); } ; friend inline istream&operator>>(istream&is,bignum&a) { read(a.num,a.sgn,is); return is ; } ; friend inline ostream&operator<<(ostream&os,const bignum&a) { if(a.sgn<0) os<<'-' ; write(a.num,os); return os ; } ; friend inline bignum sqrt(const bignum&a) { bignum ret ; bignum_t t ; memcpy(t,a.num,sizeof(bignum_t)); sqrt(ret.num,t); ret.sgn=ret.num[0]!=1||ret.num[1]; return ret ; } ; friend inline bignum sqrt(const bignum&a,bignum&b) { bignum ret ; memcpy(b.num,a.num,sizeof(bignum_t)); sqrt(ret.num,b.num); ret.sgn=ret.num[0]!=1||ret.num[1]; b.sgn=b.num[0]!=1||ret.num[1]; return ret ; } ; inline int length() { return :: length(num); } ; inline int zeronum() { return :: zeronum(num); } ; }n,m,t,A[10],B[10],ans,a0,nowt; bignum C(bignum n,bignum m) { ans=1;bignum maxx=n-m; for(int i=1;maxx>=i;++i)ans=ans*(m+i)/i; return ans; } const int MOD=3389; LL ksm(LL a,LL b) { LL ans=1; for(;b;b>>=1,a=a*a%MOD)if(b&1)ans=ans*a%MOD; return ans; } struct Matrix { int a[3][3]; Matrix(){memset(a,0,sizeof(a));} void clear(){for(int i=1;i<=2;++i)a[i][i]=1;} Matrix operator * (const Matrix &B) { Matrix C; for(int i=1;i<=2;++i) for(int j=1;j<=2;++j) for(int k=1;k<=2;++k) C.a[i][j]=(C.a[i][j]+a[i][k]*B.a[k][j])%MOD; return C; } }a; Matrix KSM(const Matrix &X,bignum b) { Matrix A=X,ans;ans.clear(); while(b>0) { if(b%2==1)ans=ans*A; A=A*A; b/=2; } return ans; } int main() { int i; a0=0; cin>>n>>t>>m;t=a0-t; int inv=ksm(1234,MOD-2); a.a[1][1]=1234;a.a[1][2]=0;a.a[2][1]=a.a[2][2]=1; a=KSM(a,n-i); int res=(a.a[1][1]+5678ll*a.a[2][1])%MOD; bignum maxx=n-m; for(i=0;maxx>=i;++i) { A[i]=res;B[i]=A[i]; nowt=1; for(int j=i-1;j>=0;--j) { nowt*=t; B[i]-=nowt*C(n-j,n-i)*B[j]; } res=(res-5678)*inv%MOD; if(res<0)res+=MOD; } cout<<B[i-1]; return 0; } ```