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