Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <algorithm>
- #include <cstring>
- #define Nmax 500
- #define Rmax 420000
- using namespace std;
- class InParser
- {
- private:
- FILE *fin;
- char *buff;
- int sp;
- char read_ch()
- {
- ++sp;
- if(sp==4096)
- {
- sp=0;
- fread(buff,1,4096,fin);
- }
- return buff[sp];
- }
- public:
- InParser(const char* nume)
- {
- fin=fopen(nume,"r");
- buff=new char[4096]();
- sp=4095;
- }
- InParser& operator >> (int &n)
- {
- char c;
- while(!isdigit(c=read_ch())&&c!='-');
- int sgn=1;
- if (c=='-')
- {
- n=0;
- sgn=-1;
- }
- else
- {
- n=c-'0';
- }
- while(isdigit(c=read_ch()))
- {
- n=10*n+c-'0';
- }
- n*=sgn;
- return *this;
- }
- InParser& operator >> (long long &n)
- {
- char c;
- n=0;
- while(!isdigit(c=read_ch())&&c!='-');
- long long sgn=1;
- if(c=='-')
- {
- n=0;
- sgn=-1;
- }
- else
- {
- n=c-'0';
- }
- while(isdigit(c=read_ch()))
- {
- n=10*n+c-'0';
- }
- n*=sgn;
- return *this;
- }
- };
- class OutParser
- {
- private:
- FILE *fout;
- char *buff;
- int sp;
- void write_ch(char ch)
- {
- if(sp==500000)
- {
- fwrite(buff,1,500000,fout);
- sp=0;
- buff[sp++]=ch;
- }
- else
- {
- buff[sp++]=ch;
- }
- }
- public:
- OutParser(const char* name)
- {
- fout=fopen(name,"w");
- buff=new char[500000]();
- sp=0;
- }
- ~OutParser()
- {
- fwrite(buff,1,sp,fout);
- fclose(fout);
- }
- OutParser& operator <<(int vu32)
- {
- if(vu32<=9)
- {
- write_ch(vu32+'0');
- }
- else
- {
- (*this) <<(vu32/10);
- write_ch(vu32%10+'0');
- }
- return *this;
- }
- OutParser& operator <<(long long vu64)
- {
- if(vu64<=9)
- {
- write_ch(vu64+'0');
- }
- else
- {
- (*this) <<(vu64/10);
- write_ch(vu64%10+'0');
- }
- return *this;
- }
- OutParser& operator <<(char ch)
- {
- write_ch(ch);
- return *this;
- }
- OutParser& operator <<(const char *ch)
- {
- while(*ch)
- {
- write_ch(*ch);
- ++ch;
- }
- return *this;
- }
- };
- InParser f("fibosir.in");
- OutParser g("fibosir.out");
- int N, M, K, la, lb, lc;
- char s[420001], a[500], b[500], c[500];
- void add(char *a, char *b, char *c)
- {
- int i = 0 , j = 0;
- int x, k, t;
- x = k = t = 0;
- while (i < la && j < lb)
- {
- x = (a[i] - 48) + (b[j] - 48) + t;
- c[k] = (x % 10) + 48;
- t = x/10;
- ++i; ++j; ++k;
- }
- while (i < la)
- {
- x = (a[i] - 48) + t;
- c[k] = (x % 10) + 48;
- t = x/10;
- ++i; ++k;
- }
- while (j < lb)
- {
- x = (b[j] - 48) + t;
- c[k] = (x % 10) + 48;
- t = x/10;
- ++j; ++k;
- }
- if (t)
- {
- c[k] = t + 48;
- ++k;
- }
- c[k] = '\0';
- lc = k;
- }
- int main()
- {
- char max;
- int i, j, poz;
- f >> N >> M >> K;
- //construim fibosir(n)
- a[0] = b[0] = s[0] = s[1] = '1';
- a[1] = b[1] = s[2] = '\0';
- la = lb = lc = 1;
- for (i=2; i<N; ++i)
- {
- add(a, b, c);
- memcpy(a, b, lb);
- memcpy(b, c, lc);
- reverse(c, c + lc);
- la = lb; lb = lc;
- strcat(s, c);
- }
- N = strlen(s);
- i = 0;
- while (i < N)
- {
- for (j=1, max = '0'; j<=M; ++j)
- {
- if (s[i + j*K] > max)
- {
- max = s[i + j*K];
- poz = j;
- if (max == '9') break;
- }
- }
- if (s[i] < max) /// jump
- {
- i += (poz * K);
- M -= poz;
- }
- else
- {
- g << s[i++];
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement