发布于  更新于 

如何用C#强行A掉洛谷高精模板(P1601)

题解 OI dotnet

众所周知,洛谷的C# Mono默认是没有引用System.Numerics这个程序集的,也就是说不能像Java或者Python一样直接调用内建的高精度类型

按照正常思路,但在洛谷会CE的程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Numerics;


namespace Solution
{
class Program
{
public static int Main(string[] args)
{
var a = BigInteger.Parse(Console.ReadLine();
var b = BigInteger.Parse(Console.ReadLine();
Console.WriteLine(a+b);
}
}
}

但是我们可以用反射技术来强行动态加载程序集!这样就可以动态调用高精度类了,详情见代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Reflection;

namespace Solution
{
class Program
{
public static void Main(string[] args)
{
try
{
var Numerics = Assembly.LoadFile("/usr/lib/mono/4.5/System.Numerics.dll");
Type BigInteger = Numerics.GetType("System.Numerics.BigInteger");
dynamic a = BigInteger.InvokeMember("Parse", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public, null, null, new object[] { Console.ReadLine() });
dynamic b = BigInteger.InvokeMember("Parse", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public, null, null, new object[] { Console.ReadLine() });
Console.WriteLine(BigInteger.InvokeMember("Multiply", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public, null, null, new object[] { a, b }).ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

顺便吐槽一下洛谷的编译器版本是mono4.6,然而运行时版本是4.5,猜了好久路径...

还有,不知道是不是反射导致的性能低下,此方法对于P1919会T掉

最后吐槽一句洛谷MarkDown编辑器的弱智自动空行,极其不适,能否给个选项关掉...