fork(2) download
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. class CodeIQ2549{
  5. const int bufsiz=32;
  6.  
  7. //MacPorts
  8. //const string libgmp="/opt/local/lib/libgmp.dylib";
  9. //ideone
  10. const string libgmp="/usr/lib/i386-linux-gnu/libgmp.so.10";
  11. //paiza.io/atcoder
  12. //const string libgmp="/usr/lib/x86_64-linux-gnu/libgmp.so.10";
  13. //wandbox/yukicoder
  14. //const string libgmp="/usr/lib64/libgmp.so.10";
  15. //AOJ
  16. //const string libgmp="/usr/lib64/libgmp.so.3";
  17.  
  18. [DllImport("msvcrt",CallingConvention=CallingConvention.Cdecl)]static extern int write(int x,string y,int z);
  19. [DllImport("msvcrt",CallingConvention=CallingConvention.Cdecl)]static extern IntPtr fopen(string x,string y);
  20. [DllImport("msvcrt",CallingConvention=CallingConvention.Cdecl)]static extern int fflush(IntPtr x);
  21. [DllImport("msvcrt",CallingConvention=CallingConvention.Cdecl)]static extern int puts(string x);
  22. [DllImport("msvcrt",CallingConvention=CallingConvention.Cdecl)]static extern int system(string x);
  23.  
  24. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_init(IntPtr x);
  25. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_clear(IntPtr x);
  26. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_out_str(IntPtr x,int y,IntPtr z);
  27. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_set_str(IntPtr x,string y,int z);
  28. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_set(IntPtr x,IntPtr y);
  29. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_set_si(IntPtr x,long y);
  30. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern int __gmpz_cmp_si(IntPtr x,long y);
  31.  
  32. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_add(IntPtr x,IntPtr y,IntPtr z);
  33. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_sub(IntPtr x,IntPtr y,IntPtr z);
  34. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_mul(IntPtr x,IntPtr y,IntPtr z);
  35. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_tdiv_q(IntPtr x,IntPtr y,IntPtr z);
  36. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_tdiv_r(IntPtr x,IntPtr y,IntPtr z);
  37. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_add_ui(IntPtr x,IntPtr y,long z);
  38. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_sub_ui(IntPtr x,IntPtr y,long z);
  39. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_mul_ui(IntPtr x,IntPtr y,long z);
  40. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_tdiv_q_ui(IntPtr x,IntPtr y,long z);
  41. [DllImport(libgmp,CallingConvention=CallingConvention.Cdecl)]static extern void __gmpz_tdiv_r_ui(IntPtr x,IntPtr y,long z);
  42.  
  43. static void mul(IntPtr a1,IntPtr b1,IntPtr c1,IntPtr d1,IntPtr a2,IntPtr b2,IntPtr c2,IntPtr d2){
  44. IntPtr a0=Marshal.AllocHGlobal(bufsiz);__gmpz_init(a0);
  45. IntPtr b0=Marshal.AllocHGlobal(bufsiz);__gmpz_init(b0);
  46. IntPtr c0=Marshal.AllocHGlobal(bufsiz);__gmpz_init(c0);
  47. IntPtr d0=Marshal.AllocHGlobal(bufsiz);__gmpz_init(d0);
  48. IntPtr t=Marshal.AllocHGlobal(bufsiz);__gmpz_init(t);
  49. IntPtr u=Marshal.AllocHGlobal(bufsiz);__gmpz_init(u);
  50.  
  51. __gmpz_mul(t,a1,a2);
  52. __gmpz_mul(u,b1,c2);
  53. __gmpz_add(a0,t,u);
  54. __gmpz_mul(t,a1,b2);
  55. __gmpz_mul(u,b1,d2);
  56. __gmpz_add(b0,t,u);
  57. __gmpz_mul(t,c1,a2);
  58. __gmpz_mul(u,d1,c2);
  59. __gmpz_add(c0,t,u);
  60. __gmpz_mul(t,c1,b2);
  61. __gmpz_mul(u,d1,d2);
  62. __gmpz_add(d0,t,u);
  63.  
  64. __gmpz_set(a1,a0);
  65. __gmpz_set(b1,b0);
  66. __gmpz_set(c1,c0);
  67. __gmpz_set(d1,d0);
  68.  
  69. __gmpz_clear(a0);Marshal.FreeHGlobal(a0);
  70. __gmpz_clear(b0);Marshal.FreeHGlobal(b0);
  71. __gmpz_clear(c0);Marshal.FreeHGlobal(c0);
  72. __gmpz_clear(d0);Marshal.FreeHGlobal(d0);
  73. __gmpz_clear(t);Marshal.FreeHGlobal(t);
  74. __gmpz_clear(u);Marshal.FreeHGlobal(u);
  75. }
  76.  
  77. static void Main(){
  78. IntPtr stdout=fopen("/dev/stdout","w");
  79.  
  80. IntPtr a1=Marshal.AllocHGlobal(bufsiz);__gmpz_init(a1);
  81. IntPtr b1=Marshal.AllocHGlobal(bufsiz);__gmpz_init(b1);
  82. IntPtr c1=Marshal.AllocHGlobal(bufsiz);__gmpz_init(c1);
  83. IntPtr d1=Marshal.AllocHGlobal(bufsiz);__gmpz_init(d1);
  84. IntPtr a2=Marshal.AllocHGlobal(bufsiz);__gmpz_init(a2);
  85. IntPtr b2=Marshal.AllocHGlobal(bufsiz);__gmpz_init(b2);
  86. IntPtr c2=Marshal.AllocHGlobal(bufsiz);__gmpz_init(c2);
  87. IntPtr d2=Marshal.AllocHGlobal(bufsiz);__gmpz_init(d2);
  88.  
  89. __gmpz_set_si(a1,1);
  90. __gmpz_set_si(b1,0);
  91. __gmpz_set_si(c1,0);
  92. __gmpz_set_si(d1,1);
  93. __gmpz_set_si(a2,1);
  94. __gmpz_set_si(b2,1);
  95. __gmpz_set_si(c2,1);
  96. __gmpz_set_si(d2,0);
  97.  
  98. int N=int.Parse(Console.ReadLine());
  99. N=(N+3)/2*2;
  100. for(;N>0;N>>=1){
  101. if((N&1)>0)mul(a1,b1,c1,d1,a2,b2,c2,d2);
  102. mul(a2,b2,c2,d2,a2,b2,c2,d2);
  103. }
  104. __gmpz_sub_ui(c1,c1,1);
  105. __gmpz_out_str(stdout,10,c1);
  106. puts("");
  107. fflush(stdout);
  108.  
  109. __gmpz_clear(a1);Marshal.FreeHGlobal(a1);
  110. __gmpz_clear(b1);Marshal.FreeHGlobal(b1);
  111. __gmpz_clear(c1);Marshal.FreeHGlobal(c1);
  112. __gmpz_clear(d1);Marshal.FreeHGlobal(d1);
  113. __gmpz_clear(a2);Marshal.FreeHGlobal(a2);
  114. __gmpz_clear(b2);Marshal.FreeHGlobal(b2);
  115. __gmpz_clear(c2);Marshal.FreeHGlobal(c2);
  116. __gmpz_clear(d2);Marshal.FreeHGlobal(d2);
  117. }
  118. }
Success #stdin #stdout 0.07s 24512KB
stdin
2015
stdout
24410294683171395267259945469996127000411199333760853190535535281681195871429510314079442068798555059453792431772087225245168879580469159794544170936403149540819320510882801573596907938222922817134288725100817648047405608500267748766714030468003650259685406411646787207097050545802045736020993909154298598218721111963426993884619351338577630868510716463423585020972878819198991971234596733617320373133963970742975210614208