您的位置:首页 > 其它

20150806解题报告

2015-08-08 22:28 393 查看

【T1】Prime

≤10^12的数中大于等于10^6的因数最多一个,所以只要把10^6以内的全部筛出来, ≥10^6的因数特判就可以了,而10^6以内素数的个数位ln10^6个,复杂度轻松不虚

#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;

typedef long long LL;
const int N = 1111111;
int tot, prime
, a
;
inline void setIO(){
freopen("prime.in", "r", stdin);
freopen("prime.out", "w", stdout);
}
inline LL read(){
LL x = 0; char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) {x = x * 10LL + 1LL * (ch - '0'); ch = getchar();}
return x;
}
inline void work(){
for (int i = 2; i <= N; i++){
if (!a[i]){ prime[++tot] = i; }
int j = 1;
do{
if (i * prime[j] > N) break;
a[i * prime[j]] = prime[j]; j++;
}while(j <= tot && i % prime[j - 1] != 0);
}
}
inline bool check(LL n){
int lim = (int)(sqrt(n));
for (int i = 1; i <= tot; i++)
if (prime[i] > lim) break;
else if (n % prime[i] == 0) return false;
return true;
}
int main(){
setIO();
int cnt; scanf("%d", &cnt);
work();
while (cnt--)
if (check(read())) printf("Prime\n");
else printf("Not prime\n");
return 0;
}

【T2】sum

在线:splay 离线:线段树

感觉代码复杂度都很大,这尼玛是NOIP题?

p`s pushdown的时候一定要注意写成 if (tag[now]) {} 的形式,因为tag可能为负,训练时就因为这个爆炸了

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long

const ll mod = 7459;
const int maxn = 222222;
ll size[maxn], sum[maxn], pf[maxn], tag[maxn], a[maxn], val[maxn];
int n, m, id[maxn], ch[maxn][2], fa[maxn], rt, sz;

inline void up(int now) {
int l = ch[now][0], r = ch[now][1];
size[now] = size[l] + size[r] + 1ll;
sum[now] = (sum[l] + sum[r] + val[now]) % mod;
pf[now] = (pf[l] + pf[r] + val[now] * val[now]) %mod;
}

inline void pushdown(int now) {
int l = ch[now][0], r = ch[now][1];
if (tag[now]!=0) {
tag[l] += tag[now]; tag[r] += tag[now];
val[l] = (val[l] + tag[now]) % mod;
pf[l] = (pf[l] + sum[l] * tag[now] * 2 % mod + tag[now] * tag[now] * size[l] %mod) %mod;
sum[l] = (sum[l] + size[l] * tag[now]) % mod;
val[r] = (val[r] + tag[now]) % mod;
pf[r] = (pf[r] + sum[r] * tag[now] * 2 % mod + tag[now] * tag[now] * size[r] %mod) %mod;
sum[r] = (sum[r] + size[r] * tag[now]) % mod;
tag[now] = 0;
}
}

inline void rotate(int x,int &now) {
int y = fa[x], z = fa[y], l, r;
if (ch[y][0] == x) l = 0; else l = 1;
r = l ^ 1;
if (y == now) now = x;
else { if (ch[z][0] == y) ch[z][0] = x; else ch[z][1] = x; }
fa[x] = z; fa[y] = x; fa[ch[x][r]] = y;
ch[y][l] = ch[x][r]; ch[x][r] = y;
up(y); up(x);
}

inline void splay(int x, int &now) {
while(x != now) {
int y = fa[x], z = fa[y];
if (y != now) {
if ((ch[y][0] == x) ^ (ch[z][0] == y))
rotate(x, now);
else
rotate(y, now);
}
rotate(x, now);
}
}
inline void build(int l, int r, int f) {
if (l > r) return;
int now = id[l], last = id[f];
if (l == r) {
val[now] = sum[now] = a[l];
pf[now] = a[l] * a[l] % mod;
fa[now] = last; size[now] = 1;
if (l < f) ch[last][0] = now;
else ch[last][1] = now;
return;
}
int mid = (l + r) >> 1; now = id[mid];
build(l, mid-1, mid); build(mid+1, r, mid);
val[now] = a[mid]; fa[now] = last; up(now);
if (mid < f) ch[last][0] = now;
else ch[last][1] = now;
return;
}

inline int find(int now, int rank) {
pushdown(now);
int l = ch[now][0], r = ch[now][1];
if (size[l] + 1 == rank) return now;
else if (size[l] >= rank) return find(l, rank);
else return find(r, rank - size[l] - 1);
}

inline void insert(int now, ll data) {
a[1] = data;
id[1] = ++sz;
build(1, 1, 0);
int z = id[1];
int x = find(rt, now), y = find(rt, now+1);
splay(x, rt); splay(y, ch[x][1]);
ch[y][0] = z; fa[z] = y;
up(y); up(x);
}

inline void add(int left, int right, int delta) {
int x = find(rt, left), y = find(rt, right+2);
splay(x, rt); splay(y, ch[x][1]);
int z = ch[y][0];
tag[z] += delta;
val[z] += delta;
pf[z] = (pf[z] + delta * 2 * sum[z] % mod + delta * delta % mod * size[z] % mod + mod) %mod;
sum[z] = (sum[z] + delta * size[z] % mod + mod) %mod;
up(y); up(x);
}

inline void query(int left, int right) {
int x = find(rt, left), y = find(rt, right+2);
splay(x, rt); splay(y, ch[x][1]);
int z = ch[y][0];
printf("%I64d\n", (pf[z] + mod) % mod);
}

int main() {
freopen("sum.in", "r", stdin);
freopen("sum.out", "w", stdout);

int k, tot;
ll val;
scanf("%d", &n);
rep(i, 1, n+2) id[i] = ++sz;
rep(i, 1, n) scanf("%I64d", &a[i+1]);
build(1, n+2, 0); rt = (n+3) >>1;

scanf("%d", &m);
while(m--) {
char s[10]; scanf("%s", s);
switch(s[0]) {
case 'I' : scanf("%d%I64d", &k, &val); insert(k, val); break;
case 'A' : scanf("%d%d%I64d", &k, &tot, &val); add(k, tot, val); break;
case 'Q' : scanf("%d%d", &k, &tot); query(k, tot); break;
}
}
return 0;
}

【T3】tree

树形dp。记录子树状态:0黑0白,0黑1白,0黑2白,1黑0白,1黑1白。

直接做转移太麻烦,可以预处理两状态合并后的状态,5^2转移。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: