aydarbiktimirov

11

Oct 19th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4.  
  5. struct long_int
  6. {
  7.     bool positive;
  8.     char data[200];
  9. };
  10.  
  11. void print_long_int(const long_int *a)
  12. {
  13.     if (!a->positive)
  14.     {
  15.         printf("-");
  16.     }
  17.     int i = 0;
  18.     while (i < 199 && a->data[i] == 0)
  19.     {
  20.         ++i;
  21.     }
  22.     do
  23.     {
  24.         printf("%d", a->data[i++]);
  25.     } while (i < 200);
  26. }
  27.  
  28. int compare_long_int(const long_int *a, const long_int *b)
  29. {
  30.     if (a->positive && !b->positive)
  31.     {
  32.         return 1;
  33.     }
  34.     if (!a->positive && b->positive)
  35.     {
  36.         return -1;
  37.     }
  38.     int i = 0, j = 0;
  39.     while (i < 200 && a->data[i] == 0)
  40.     {
  41.         ++i;
  42.     }
  43.     while (j < 200 && b->data[j] == 0)
  44.     {
  45.         ++j;
  46.     }
  47.     if (i > j)
  48.     {
  49.         return a->positive ? -1 : 1;
  50.     }
  51.     if (i < j)
  52.     {
  53.         return a->positive ? 1 : -1;
  54.     }
  55.     if (i == j)
  56.     {
  57.         while (i < 200 && a->data[i] == b->data[i])
  58.         {
  59.             ++i;
  60.         }
  61.         if (i == 200)
  62.         {
  63.             return 0;
  64.         }
  65.         return a->data[i] > b->data[j] ? (a->positive ? 1 : -1) : (a->positive ? -1 : 1);
  66.     }
  67. }
  68.  
  69. void read_long_int(long_int *a)
  70. {
  71.     char tmp[200], c;
  72.     int cnt = 0;
  73.     c = getchar();
  74.     a->positive = c != '-';
  75.     if (!a->positive)
  76.     {
  77.         c = getchar();
  78.     }
  79.     do
  80.     {
  81.         tmp[cnt++] = c - '0';
  82.     } while ((c = getchar()) >= '0' && c <= '9');
  83.     for (int i = 0; i < cnt; ++i)
  84.     {
  85.         a->data[199 - i] = tmp[cnt - 1 - i];
  86.     }
  87. }
  88.  
  89. void make_long_int(long_int *a)
  90. {
  91.     for (int i = 0; i < 200; ++i)
  92.     {
  93.         a->data[i] = 0;
  94.     }
  95.     a->positive = true;
  96. }
  97.  
  98. void copy_long_int(long_int *res, const long_int *a)
  99. {
  100.     for (int i = 0; i < 200; ++i)
  101.     {
  102.         res->data[i] = a->data[i];
  103.     }
  104. }
  105.  
  106. void abs_long_int(long_int *res, const long_int *a)
  107. {
  108.     copy_long_int(res, a);
  109.     res->positive = true;
  110. }
  111.  
  112. void add_long_int(long_int *res, const long_int *a, const long_int *b);
  113.  
  114. void minus_long_int(long_int *res, const long_int *a, const long_int *b)
  115. {
  116.     long_int tmp;
  117.     char j = 0;
  118.     if (!b->positive)
  119.     {
  120.         abs_long_int(&tmp, b);
  121.         add_long_int(res, a, &tmp);
  122.     } else {
  123.         abs_long_int(&tmp, a);
  124.         if (!a->positive)
  125.         {
  126.             add_long_int(res, b, &tmp);
  127.             res->positive = false;
  128.         } else {
  129.             if (compare_long_int(a, b) == -1)
  130.             {
  131.                 minus_long_int(res, b, a);
  132.                 res->positive = false;
  133.             } else {
  134.                 for (int i = 199; i >= 0; --i)
  135.                 {
  136.                     j = 0;
  137.                     if (tmp.data[i] >= b->data[i])
  138.                     {
  139.                         tmp.data[i] = tmp.data[i] - b->data[i];
  140.                     } else {
  141.                         j = 1;
  142.                         while (i + j < 200 && tmp.data[i - j] == 0)
  143.                         {
  144.                             tmp.data[i - ++j] = 9;
  145.                         }
  146.                         --tmp.data[i - j];
  147.                         tmp.data[i] = 10 + tmp.data[i] - b->data[i];
  148.                     }
  149.                 }
  150.                 res->positive = compare_long_int(a, b) >= 0;
  151.                 for (int i = 0; i < 200; ++i)
  152.                 {
  153.                     res->data[i] = tmp.data[i];
  154.                 }
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. void add_long_int(long_int *res, const long_int *a, const long_int *b)
  161. {
  162.     char tmp[200], x = 0;
  163.     if (a->positive == b->positive)
  164.     {
  165.         for (int i = 199; i >= 0; --i)
  166.         {
  167.             tmp[i] = (a->data[i] + b->data[i] + x) % 10;
  168.             x = (a->data[i] + b->data[i] + x) / 10;
  169.         }
  170.         for (int i = 0; i < 200; ++i)
  171.         {
  172.             res->data[i] = tmp[i];
  173.         }
  174.     } else {
  175.         long_int x;
  176.         if (!a->positive)
  177.         {
  178.             abs_long_int(&x, a);
  179.             minus_long_int(res, b, &x);
  180.         } else {
  181.             abs_long_int(&x, b);
  182.             minus_long_int(res, a, &x);
  183.         }
  184.     }
  185. }
  186.  
  187. int main()
  188. {
  189.     int n, k;
  190.     long_int *a;
  191.     freopen("INPUT.TXT", "r", stdin);
  192.     freopen("OUTPUT.TXT", "w", stdout);
  193.     scanf("%d %d", &k, &n);
  194.     a = (long_int *)malloc(sizeof(long_int) * (n + 1));
  195.     memset(a, 0, sizeof(long_int) * (n + 1));
  196.     a[n].data[199] = 1;
  197.     a[n].positive = true;
  198.     for (int i = n - 1; i >= 0; --i)
  199.     {
  200.         for (int j = 0; j < k; ++j)
  201.         {
  202.             if (i - j >= 0)
  203.             {
  204.                 //a[i - j] += a[i + 1];
  205.                 add_long_int(&a[i - j], &a[i - j], &a[i + 1]);
  206.             }
  207.         }
  208.     }
  209.     //printf("%d\n", a[0]);
  210.     print_long_int(&a[0]);
  211.     printf("\n");
  212.     free(a);
  213.     return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment