5.30
T1 函数求和
- 15年论文里的题目,分块乱搞
- 我们可以对原序列和f序列都分块
- 在O(nsqrt(n))的时间内处理出f序列中的每一块里面对应a序列中的每个数分别出现了多少次,在修改的时候整块可以直接修改。同时我们维护一下原序列中每块内的前缀和和总体的前缀和。这样查询的时候不是整块内的f可以O(1)的查询(详见代码)
#define MAXN 100010UL
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
int n, Q, num, bn[MAXN][2], bg[MAXN], ti[330][MAXN], bk[330][2], a[MAXN];
ll v[410], sum[MAXN];
vector <int> v1[MAXN], v2[MAXN];
void Modify(int x, int y) {
for(int i = 1 ; i <= num ; ++ i) v[i] += 1ll*ti[i][x]*(y-a[x]);
for(int i = x ; i <= bk[bg[x]][1] ; ++ i) sum[i] += (ll)(y-a[x]);
for(int i = bg[x]+1 ; i <= num ; ++ i) sum[bk[i][1]] += (ll)(y-a[x]);
a[x] = y;
return;
}
ll Ask(int x) {
if(x==bk[bg[x]][1]) return sum[x];
return sum[bk[bg[x]-1][1]]+sum[x];
}
ll Solve(int l, int r) {
int lt = bg[l], rt = bg[r];
ll ans = 0;
for(int i = lt+1 ; i < rt ; ++ i) ans += v[i];
for(int i = l, R = min(bk[lt][1], r) ; i <= R ; ++ i) ans += Ask(bn[i][1])-Ask(bn[i][0]-1);
if(lt<rt) {
for(int i = bk[rt][0] ; i <= r ; ++ i) ans += Ask(bn[i][1])-Ask(bn[i][0]-1);
}
return ans;
}
void In(int &x) {
x = 0;
char tmp = getchar();
while(tmp<'0'||tmp>'9') tmp = getchar();
while(tmp>='0'&&tmp<='9') x = x*10+tmp-'0', tmp = getchar();
return;
}
int main() {
// freopen("sum.in", "r", stdin);
// freopen("sum.out", "w", stdout);
int op, x, y;
In(n);
for(int i = 1 ; i <= n ; ++ i) In(a[i]);
int blo = sqrt(n);
for(int i = 1 ; i <= n ; ++ i) {
In(bn[i][0]), In(bn[i][1]);
v1[bn[i][0]].push_back(i), v2[bn[i][1]].push_back(i);
bg[i] = (i-1)/blo+1;
if(bg[i]!=bg[i-1]) {
++ num, bk[num][0] = i;
bk[num-1][1] = i-1;
}
}
bk[num][1] = n;
for(int i = 1 ; i <= n ; ++ i) {
for(int j = 1 ; j <= num ; ++ j) ti[j][i] = ti[j][i-1];
for(int j = 0, r = v2[i-1].size() ; j < r ; ++ j) -- ti[bg[v2[i-1][j]]][i];
for(int j = 0, r = v1[i].size() ; j < r ; ++ j) ++ ti[bg[v1[i][j]]][i];
}
for(int i = 1 ; i <= num ; ++ i) {
sum[bk[i][0]] = a[bk[i][0]];
for(int j = bk[i][0]+1 ; j <= bk[i][1] ; ++ j) sum[j] = (ll)a[j]+sum[j-1];
sum[bk[i][1]] += sum[bk[i-1][1]];
}
for(int i = 1 ; i <= num ; ++ i) {
for(int j = bk[i][0] ; j <= bk[i][1] ; ++ j) v[i] += Ask(bn[j][1])-Ask(bn[j][0]-1);
}
In(Q);
for(int i = 1 ; i <= Q ; ++ i) {
In(op), In(x), In(y);
if(op==1) Modify(x, y);
else printf("%llu\n", Solve(x, y));
}
return 0;
}
T2 解方程
- sdoi2013原题 (无力吐槽)
- 首先后面那个限制可以直接忽略掉,原本就有>=1的限制,一样做就可以
- 剩下的就是小于等于的限制,我们用总的方案减去不合法的方案的得到的就是答案,这一步可以容斥
- 因为模数并不是质数,所以要写CRT (以前一直不想写,所以坑着,今天终于遭报应了).
#define MAXN 100010UL
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
struct pii {
ll rest, power;
pii() {
power = 0, rest = 1;
return;
}
};
ll n, n1, n2, m, Mod, ans, fac[MAXN];
ll a[20];
ll Ksm(ll x, ll k, ll mod) {
ll ret = 1; x %= mod;
while(k) {
if(k&1) ret = ret*x%mod;
x = x*x%mod;
k >>= 1;
}
return ret;
}
ll Phi(ll x) {
if(x==1) return 1;
ll ans = x;
for(ll i = 2, r = sqrt(x) ; i <= r ; ++ i) {
if(x%i==0) ans /= i, ans *= i-1;
while(x%i==0) x /= i;
}
if(x!=1) ans /= x, ans *= x-1ll;
return ans;
}
pii Get(ll s, ll p, ll pc) {
pii ret;
while(s) {
ret.rest = ret.rest*Ksm(fac[pc], s/pc, pc)%pc;
ret.rest = ret.rest*fac[s%pc]%pc;
s /= p, ret.power += s;
}
return ret;
}
struct ME {
ll n, m;
void Pre_fac(ll p, ll pc) {
fac[0] = 1ll;
for(ll i = 1 ; i <= pc ; ++ i) {
if(i%p==0) fac[i] = fac[i-1];
else fac[i] = (fac[i-1]*i)%pc;
}
return;
}
ll C(ll p, ll c, ll pc) {
ll ans; Pre_fac(p, pc);
pii k1 = Get(n, p, pc), k2 = Get(m, p, pc), k3 = Get(n-m, p, pc);
ans = Ksm(p, k1.power-k2.power-k3.power, pc);
ans = ans*k1.rest%pc;
ans = ans*Ksm(k2.rest*k3.rest, Phi(pc)-1, pc)%pc;
return ans;
}
ll Work(ll _n,ll _m, ll _p) {
n = _n, m = _m;
ll p = _p, ans = 0;
if(n<m) return 0;
if(m==0) return 1;
for(ll i = 2, r = sqrt(_p) ; i <= r ; ++ i) {
if(p%i) continue;
ll c = 0, pc = 1ll;
while(p%i==0) p /= i, ++ c, pc *= i;
(ans += (C(i, c, pc)*(_p/pc)%_p)*Ksm(_p/pc, Phi(pc)-1, pc)%_p) %= _p;
}
if(p!=1) (ans += (C(p, 1, p)*(_p/p)%_p)*Ksm(_p/p, Phi(p)-1, p)%_p) %= _p;
return ans;
}
}st;
void Dfs(int x, ll sum, int k) {
if(m-sum-(n-n2-k)<0) return;
if(k&1) ans -= st.Work(m-sum-(n-n2-k)+n-1, n-1, Mod)%Mod;
else ans += st.Work(m-sum-(n-n2-k)+n-1, n-1, Mod)%Mod;
for(int i = x+1 ; i <= n1 ; ++ i) Dfs(i, sum+a[i]+1ll, k+1);
return;
}
int main() {
int T; scanf("%d%lld", &T, &Mod);
while(T --) {
scanf("%lld%lld%lld%lld", &n, &n1, &n2, &m);
ans = 0;
ll sum = 0;
for(int i = 1 ; i <= n1+n2 ; ++ i) {
scanf("%lld", &a[i]);
if(i>n1) sum += a[i];
}
Dfs(0, sum, 0);
ans = (ans%Mod+Mod)%Mod;
printf("%lld\n", ans);
}
// while(1);
return 0;
}
T3 宇宙序列
- 看到异或一脸FWT的样子,实际就是FWT
- 对于这个序列,我们可以先把它的点值表达式搞出来,然后就转化成了求从某一个数开始的不断平方的和,逆FWT回去就得到答案了,然后这个东西可以倍增
- 还可以考虑另外一种做法,因为有模数,所以一个数不断平方的过程肯定会出现环,我们可以把所有的环找出来,对于某个数来说它多次平方后走到环上的部分可以直接算,而这个题由于模数的特殊,只有一个大环连着一堆长度为1的链,还有 0 和 1 两个小环
#define MAXN 300010UL
#include <cstdio>
#define Mod 10007
using namespace std;
typedef long long ll;
int n, p, J, A[MAXN], f[Mod+10][40], g[Mod+10][40];
int Ksm(int x, int k, int mod) {
int ret = 1;
while(k) {
if(k&1) ret = ret*x%mod;
x = x*x%mod;
k >>= 1;
}
return ret;
}
void FWT(int *r, int n, int val) {
for(int k = 2 ; k <= n ; k <<= 1) {
for(int i = 0 ; i < n ; i += k) {
for(int j = 0 ; j < (k>>1) ; ++ j) {
int u = r[i+j], t = r[i+j+(k>>1)];
r[i+j] = 1ll*(u+t)*val%Mod, r[i+j+(k>>1)] = 1ll*(u-t+Mod)*val%Mod;
}
}
}
return;
}
int Get_(int x, int k) {
int ret = 0;
for(int lg = 34 ; lg >= 0 ; -- lg) if(k>=(1ll<<lg)) {
ret += f[x][lg];
if(ret>=Mod) ret -= Mod;
x = g[x][lg], k -= (1ll<<lg);
}
return ret;
}
int main() {
int inv_2 = Ksm(2, Mod-2, Mod);
scanf("%d%d%d", &n, &p, &J);
for(int i = 0 ; i < (1<<n) ; ++ i) scanf("%d", &A[i]);
for(int i = 0 ; i < Mod ; ++ i) g[i][0] = i*i%Mod, f[i][0] = i*i%Mod;
for(int j = 1 ; j <= 34 ; ++ j) {
for(int i = 0 ; i < Mod ; ++ i) {
g[i][j] = g[g[i][j-1]][j-1];
f[i][j] = f[i][j-1]+f[g[i][j-1]][j-1];
if(f[i][j]>=Mod) f[i][j] -= Mod;
}
}
FWT(A, 1<<n, 1);
for(int i = 0 ; i < (1<<n) ; ++ i) {
A[i] += Get_(A[i], p);
if(A[i]>=Mod) A[i] -= Mod;
}
FWT(A, 1<<n, inv_2);
printf("%d", A[J]);
// while(1);
return 0;
}