求助dalao们,51nod1170实在是不会了,有没有大佬讲一下,有兴趣的可以告诉我,我提供AC代码。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics; namespace Problem51Nod
{
class Program
{
public static void Main(String[] args)
{
//BigInteger m = BigInteger.Pow(5, 143) - 1;
//BigInteger n = BigInteger.Pow(2, 331) + 1;
//int k = 100;
BigInteger m = BigInteger.Parse(Console.ReadLine());
BigInteger n = BigInteger.Parse(Console.ReadLine());
int k = Convert.ToInt32(Console.ReadLine()); //Stopwatch timer = new Stopwatch();
//timer.Start();
Console.WriteLine(Cmn(m, n, k));
//timer.Stop();
//Console.WriteLine(timer.Elapsed);
} static BigInteger[][] PowerTable = new BigInteger[][];
static List<BigInteger[][]>[] PreTable = new List<BigInteger[][]>[];
static BigInteger[,] CMatrix; static void InitCMatrix(int l)
{
CMatrix = new BigInteger[l + , l + ];
for (int i = ; i <= l; i++)
CMatrix[i, ] = CMatrix[i, i] = ; for (int i = ; i <= l; i++)
for (int j = ; j * <= i; j++)
CMatrix[i, j] = CMatrix[i, i - j] = CMatrix[i - , j - ] + CMatrix[i - , j];
} static void InitPowerTab(int l, int p)
{
PowerTable[p] = new BigInteger[l + ];
PowerTable[p][] = ; for (int j = ; j < PowerTable[p].Length; j++)
PowerTable[p][j] = PowerTable[p][j - ] * p;
} static void InitData(int l, BigInteger max, int p)
{
PreTable[p] = new List<BigInteger[][]>();
BigInteger[] pre = MulTo(l, p);
BigInteger[,] cMatrix = new BigInteger[l + , l + ]; for (int i = ; i <= l; i++)
for (int j = ; j <= i; j++)
cMatrix[i, j] = CMatrix[i, j]; int power = ;
for (BigInteger i = p; i <= max; i *= p)
{
BigInteger[][] cTable = new BigInteger[p - ][];
cTable[] = pre;
for (int j = ; j < p; j++)
{
BigInteger[] next = ReplaceWith(cTable[], i * j % PowerTable[p][l], l, p, cMatrix); if (j < p - )
cTable[j] = MulMod(cTable[j - ], next, l, p);
else
pre = MulMod(cTable[j - ], next, l, p);
} PreTable[p].Add(cTable);
power++;
}
} static BigInteger Cmn(BigInteger m, BigInteger n, int l)
{
InitCMatrix(l);
InitPowerTab(l, );
InitPowerTab(l, );
BigInteger up5 = CalP(m, n, , l);
BigInteger up2 = CalP(m, n, , l);
BigInteger mod = ((up5 - up2) + PowerTable[][l]) % PowerTable[][l];
mod = IMod(PowerTable[][l], mod, PowerTable[][l]) * PowerTable[][l] + up2;
return mod;
} static BigInteger CalP(BigInteger m, BigInteger n, int p, int l)
{
BigInteger count = Count(m, p) - Count(n, p) - Count(m - n, p);
if (count > l) return ;
InitData(l, m, p);
BigInteger up = Cal(m, p, l) * PowerTable[p][(int)count];
BigInteger down = Cal(n, p, l);
down *= Cal(m - n, p, l);
down %= PowerTable[p][l];
up = IMod(down, up, PowerTable[p][l]);
return up;
} static BigInteger Count(BigInteger v, BigInteger mod)
{
BigInteger count = ; while (v > )
{
v /= mod;
count += v;
} return count;
} static List<BigInteger> Values = new List<BigInteger>();
static List<int> Mods = new List<int>(); static BigInteger Cal(BigInteger nums, int p, int l)
{
BigInteger numsBak = nums;
Values.Clear();
Mods.Clear(); while (numsBak > )
{
Values.Add(numsBak);
Mods.Add((int)(numsBak % p));
numsBak /= p;
} BigInteger result = ; for (int i = ; i < Values.Count; i++)
{
result *= CalSingle(i, p, l);
result %= PowerTable[p][l];
} return result;
} static BigInteger CalSingle(int cIndex, int p, int l)
{
int len = Mods[cIndex];
BigInteger sum = , last = (Values[cIndex] - len) % PowerTable[p][l];
BigInteger[] pre = new BigInteger[] { };
BigInteger result = ;
cIndex++; for (int i = Mods.Count - ; i >= cIndex; i--)
{
int index = Mods[i]; if (index > )
{
BigInteger modValue = , current = ;
foreach (var item in PreTable[p][i - cIndex][index - ])
{
if (item != )
current = (current + modValue * item) % PowerTable[p][l]; if (sum == || modValue == )
break; modValue = modValue * sum % PowerTable[p][l];
} if (i - cIndex + <= l)
sum = (sum + index * PowerTable[p][i - cIndex + ]) % PowerTable[p][l]; result = (result * current) % PowerTable[p][l];
}
} for (int i = ; i <= len; i++)
{
if (i % p == )
continue; result *= last + i;
result %= PowerTable[p][l];
} return result;
} static BigInteger[] MulTo(int l, int p)
{
BigInteger[] result = new BigInteger[] { }; for (BigInteger i = ; i < p; i++)
{
if (i % p == )
continue; BigInteger[] b = new BigInteger[] { i, };
result = MulMod(result, b, l, p);
} return result;
} static BigInteger[] MulMod(BigInteger[] a, BigInteger[] b, int l, int p)
{
int len = Math.Min(l + , a.Length + b.Length - );
BigInteger[] result = new BigInteger[len]; for (int i = ; i < Math.Min(a.Length, l + ); i++)
{
if (a[i] == )
continue; int upper = Math.Min(b.Length, l - i + );
for (int j = ; j < upper; j++)
result[i + j] += a[i] * b[j];
} int last = ; for (int i = ; i < result.Length; i++)
{
result[i] %= PowerTable[p][l - i];
if (result[i] > )
last = i + ;
} if (last < result.Length)
Array.Resize(ref result, last); return result;
} //2次方算法,考虑大数是3次方 除以大进制的常数
static BigInteger[] ReplaceWith(BigInteger[] source, BigInteger into, int l, int p, BigInteger[,] cMatrix)
{
int len = source.Length;
len = Math.Min(len, l + );
BigInteger[] result = new BigInteger[len];
BigInteger[] power = new BigInteger[len];
power[] = ; for (int i = ; i < len; i++)
power[i] = power[i - ] * into % PowerTable[p][l]; for (int i = ; i < len; i++)
{
if (source[i] == )
continue; for (int j = ; j <= i; j++)
{
if (power[i] == ) continue;
var tmp = power[i - j] * source[i] % PowerTable[p][l - j];
if (tmp == ) continue;
result[j] += tmp * cMatrix[i, j];
}
} for (int i = ; i < result.Length; i++)
result[i] %= PowerTable[p][l - i]; return result;
} public static BigInteger EuclidExtend(BigInteger X, BigInteger Y, out BigInteger A, out BigInteger B)
{
if (Y == ) { A = ; B = ; return X; }
BigInteger quotient = X / Y;
BigInteger gcd = EuclidExtend(Y, X - Y * quotient, out A, out B);
BigInteger Temp = A; A = B; B = Temp - quotient * A;
return gcd;
} public static bool Linear(BigInteger X, BigInteger Y, BigInteger N, out BigInteger xResult, out BigInteger yResult)
{
BigInteger gcd = EuclidExtend(X, Y, out xResult, out yResult);
if (N % gcd != ) { return false; }
xResult = xResult * N / gcd % Y;
xResult = xResult >= ? xResult : xResult + Y;
yResult = yResult * N / gcd % X;
yResult = yResult <= ? yResult : yResult - X;
return true;
} public static BigInteger IMod(BigInteger A, BigInteger B, BigInteger P)
{
BigInteger x, y;
Linear(A, P, B, out x, out y);
return x;
}
}
}

51nod求助的更多相关文章

  1. 51Nod 算法马拉松15 记一次悲壮而又开心的骗分比赛

    OwO 故事的起源大概是zcg前天发现51Nod晚上有场马拉松,然后他就很开心的过去打了 神奇的故事就开始了: 晚上的时候我当时貌似正在写线段树?然后看见zcg一脸激动告诉我第一题有九个点直接输出B就 ...

  2. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  3. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

  4. 51Nod 1428 活动安排问题

    51Nod   1428  活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...

  5. 51Nod 1278 相离的圆

    51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...

  6. 【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1501 dp求出环状不连续的前缀和,剩下东西都可以算出来,比较繁琐. 时间 ...

  7. 【51Nod 1622】【算法马拉松 19C】集合对

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1622 简单题..直接暴力快速幂 #include<cstdio&g ...

  8. 【51Nod 1616】【算法马拉松 19B】最小集合

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1616 这道题主要是查询一个数是不是原有集合的一个子集的所有数的gcd. ...

  9. 【51Nod 1674】【算法马拉松 19A】区间的价值 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1674 对区间分治,统计\([l,r]\)中经过mid的区间的答案. 我的 ...

随机推荐

  1. Loadrunner教程--常用操做流程

    1loadrunner压力测试一般使用流程 1.1loadrunner压力测试原理 本质就是在loadrunner上模拟多个用户同时按固定行为访问web站点.其中固定行为在loadrunner中是通过 ...

  2. LearnPython - Zip格式文件的解压缩

    import zipfile import os def unzip(zip_name, target_dir): files = zipfile.ZipFile(zip_name) for zip_ ...

  3. 剑指 Offer——数字在排序数组中出现的次数

    1. 题目 2. 解答 时间复杂度为 \(O(n)\) 的算法,顺序遍历数组,当该数字第一次出现时开始记录次数. class Solution { public: int GetNumberOfK(v ...

  4. Hyperledger Fabric中的Identity

    Hyperledger Fabric中的Identity 什么是Identity 区块链网络中存在如下的角色:peers, orderers, client application, administ ...

  5. 亚马逊首次推出卖家APP 可掌握商品盈利状况

    美国零售巨头亚马逊近日首次对外发布了第一款针对卖家和商户的客户端,帮助他们更加高效的管理商品和销售数据. 据美国科技新闻网站 Mashable 报道,之前亚马逊在商户移动客户端方面一直空缺,许多商户不 ...

  6. java-HttpGetPost-图片字节流上传

    在java程序开发中经常用到与服务端的交互工作,主要的就是传递相应的参数请求从而获取到对应的结果加以处理 可以使用Get请求与Post请求,注意!这里的Get请求不是通过浏览器界面而是在程序代码中设置 ...

  7. 团队Alpha冲刺(六)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  8. 福大软工1816:Beta(3/7)

    Beta 冲刺 (3/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 参与开发关键词提醒部分 展示GitHu ...

  9. TCP系列41—拥塞控制—4、Linux中的慢启动和拥塞避免(一)

    一.Linux中的慢启动和拥塞避免 Linux中采用了Google论文的建议把IW初始化成了10了.在linux中一般有三种场景会触发慢启动过程 1.连接初始建立发送数据的时候,此时cwnd初始化为1 ...

  10. find . -name file -exec echo abc > {} \; fail

    find . -name file -exec echo abc > {} \; fail 应该改用: find . -name file -exec bash -c 'echo abc > ...