P4345 [SHOI2015]超能粒子炮·改
题意:
求解式子∑i=0kCni%p\sum_{i=0}^{k}C_{n}^{i} \% p∑i=0kCni%p
n,k<=1e18
题解:
设f(n,k)=∑i=0kCnif(n,k)=\sum_{i=0}^{k}C_{n}^{i}f(n,k)=∑i=0kCni
开始化简:
由卢卡斯定理得:
f(n,k)=∑i=0kCni=∑i=0kCn/pi/p∗Cn%pi%pf(n,k)=\sum_{i=0}^{k}C_{n}^{i}=\sum_{i=0}^{k}C_{n/p}^{i/p}*C_{n\%p}^{i\%p}f(n,k)=∑i=0kCni=∑i=0kCn/pi/p∗Cn%pi%p
我们将相同的Cn/pi/pC_{n/p}^{i/p}Cn/pi/p为一组将其展开:
Cn/p0∑i=0p−1Cn%pi=Cn/p1∑i=0p−1Cn%pi+...+Cn/pk/p∑i=0k%pCn%piC_{n/p}^{0}\sum_{i=0}^{p-1}C_{n\%p}^{i}=C_{n/p}^{1}\sum_{i=0}^{p-1}C_{n\%p}^{i}+...+C_{n/p}^{k/p}\sum_{i=0}^{k\%p}C_{n\%p}^{i}Cn/p0∑i=0p−1Cn%pi=Cn/p1∑i=0p−1Cn%pi+...+Cn/pk/p∑i=0k%pCn%pi
此时有0到k/p-1这些整块,还有k/p这个不完整的块
我们先考虑完整的块,
我们将∑i=0p−1Cn%pi\sum_{i=0}^{p-1}C_{n\%p}^{i}∑i=0p−1Cn%pi提出来
变成:∑i=0p−1Cn%pi∗(Cn/p0+...+Cn/pk/p−1)\sum_{i=0}^{p-1}C_{n\%p}^{i}*(C_{n/p}^{0}+...+C_{n/p}^{k/p-1})∑i=0p−1Cn%pi∗(Cn/p0+...+Cn/pk/p−1)
我们一开始定义:
f(n,k)=∑i=0kCnif(n,k)=\sum_{i=0}^{k}C_{n}^{i}f(n,k)=∑i=0kCni
这就可以写成:
f(n%p,p−1)∗f(n/p,k/p−1)f(n\%p,p-1)*f(n/p,k/p-1)f(n%p,p−1)∗f(n/p,k/p−1)
再考虑不完整的第k/p块
∑i=0k%pCn%pi\sum_{i=0}^{k\%p}C_{n\%p}^{i}∑i=0k%pCn%pi也可以写成f(n%p,k%p)f(n\%p,k\%p)f(n%p,k%p)
总结:
f(n,k)=f(n%p,p−1)∗f(n/p,k/p−1)+Cn/pk/p∗f(n%p,k%p)f(n,k)=f(n\%p,p-1)*f(n/p,k/p-1)+C_{n/p}^{k/p}*f(n\%p,k\%p)f(n,k)=f(n%p,p−1)∗f(n/p,k/p−1)+Cn/pk/p∗f(n%p,k%p)
Cn/pk/pC_{n/p}^{k/p}Cn/pk/p可以用Lucas求
f(n%p,p−1)f(n\%p,p-1)f(n%p,p−1)和f(n%p,k%p)f(n\%p,k\%p)f(n%p,k%p)因为数值不大,直接预初理就行(杨辉三角再求和)
复杂度O(p2+Tlog23332n)O(p^{2}+Tlog^{2}_{2333}n)O(p2+Tlog23332n)
代码:
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 3000;
const int P= 2333;
ll c[maxn + 2][maxn + 2];
ll f[maxn + 2][maxn + 2];
inline ll Lucas(ll a, ll b)
{if (!b)return 1;if (a == b)return 1;if (a < b)return 0;return c[a % P][b % P] * Lucas(a / P, b / P) % P;
}
inline ll F(ll a, ll k)
{if (k < 0)return 0;if (!a)return 1;if (!k)return 1;if (a < P && k < P)return f[a][k];return (f[a % P][P - 1] * F(a / P, k / P - 1) % P + Lucas(a / P, k / P) * f[a % P][k % P] % P) % P;
}
int main()
{int T;scanf("%d", &T);c[0][0]= 1;for (int i= 1; i <= maxn; i++)c[i][i]= c[i][0]= 1;for (int i= 1; i <= maxn; i++)for (int j= 1; j < i; j++)c[i][j]= (c[i - 1][j] + c[i - 1][j - 1]) % P;f[0][0]= 1;for (int i= 1; i <= maxn; i++)f[i][0]= 1;for (int i= 0; i <= maxn; i++)for (int j= 1; j <= maxn; j++)f[i][j]= (c[i][j] + f[i][j - 1]) % P;ll a, k;while (T--) {scanf("%lld%lld", &a, &k);printf("%lld\n", F(a, k));}return 0;
}