为了解决这样的问题,我们需要关于一个整型数组A[1..N]的数据结构,它支持这样的操作,起初,数组中的元素都是0,而且这样的数据结构支持下面两种操作:
1.更新A[pos] = A[pos] + value (其中value,pos是任意给定的,且可能为负值)
2.计算sum(A, l, r) = A[l]+A[l+1]+A[l+2]+...+A[r-1]+A[r] (其中l, r是任意给定的)
我们需要这样的操作都在O(log N)的时间内完成。能够实现这样操作的数据结构是 Binary Idexed Trees (BIT)。
在我们的解决方案中我们需要两个树状数组(BIT), 下标从1..L。其中第一个叫做cnt, cnt[i] 是当前种在i这个位置中树的个数(我们增加将所有树的下标增加1,使得所有的下标在1..L内)。第二个数组叫做dst, dst[i] = cnt[i]*i。换句话说,dst[i]是所有种在位置i的树的距离和。
假设我们要在x[i]处种一棵树, 并且想要尽快计算出他的price。我们有CL=sum(cnt, 1, x[i]-1) 棵树在x[i]的左边。每个种在x0处的树距离第i棵树的距离是x[i]-x0。所以左边所有树到第i棵树的距离和就是 CL*x[i]-sum(all x0) = CL*x[i]-sum(dst, 1, x[i]-1)。同样的有CR=sum(cnt, x[i]+1, L)棵树在第i棵树的右边,他们到第i棵树的距离和是 sum(dst, x[i]+1, L)-CR*x[i]。所以总的price 可以由下式计算:

Code
price = x[i] * (sum(cnt, 1, x[i]-1) - sum(cnt, x[i]+1, L)) +
sum(dst, x[i]+1, L) - sum(dst, 1, x[i]-1
计算好price后我们只需要更新cnt, dst数组, cnt[x[i]]=cnt[x[i]]+1, dst[x[i]]=dst[x[i]]+x[i]。

Code
class ProductOfPrices {
public:
int product(int, int, int, int, int);
};
int L;
int64 cnt[200001], dst[200001];
int64 x[200001];
void add(int64 a[], int64 i, int64 x)
{
while (i <= L)
{
a[i] += x;
i |= i-1;
i++;
}
}
int64 getSum(int64 a[], int64 i)
{
int64 res = 0;
while (i > 0)
{
res += a[i];
i &=i-1;
}
return res;
}
int64 getSum(int64 a[], int64 i, int64 j)
{
if (j >= i)
return getSum(a, j) - getSum(a, i-1);
else
return 0;
}
int ProductOfPrices::product(int N, int l, int X0, int A, int B)
{
L = l;
x[0] = X0%L;
for (int i = 1; i < N; i++)
x[i] = ((int64(x[i-1])*int64(A))+int64(B)) % int64(L);
memset(cnt, 0, sizeof(cnt));
memset(dst, 0, sizeof(dst));
add(cnt, x[0]+1, 1);
add(dst, x[0]+1, x[0]+1);
int64 res = 1;
for (int i = 1; i < N; i++)
{
int64 price = (x[i]+1) * getSum(cnt, 1, x[i]) - getSum(dst, 1, x[i])
+ getSum(dst, x[i]+1, L) - getSum(cnt, x[i]+1, L) * (x[i]+1);
price %= 1000000007;
res = (res * price) % 1000000007;
add(cnt, x[i]+1, 1);
add(dst, x[i]+1, x[i]+1);
}
return (int)res;
C/C++ 模式
指定为C++模式的方法:一般根据扩展名自动设定,不用指定,不过有时候你希望.h文件是C++模式的(缺省是C模式),
在文件第一行(或其末尾)上加入
// -- C++ --
语法高亮:不是C模式专有,顺便提一下,M-x global-font-lock-mode RET 或在.emacs中加入(global-font-lock-mode t)。
在console下,Emacs21才支持语法高亮。(语法高亮,不同关键字用不同的颜色来突出)
子模式:
-
auto-state 当你输入时自动缩进,自动换行
-
hungry-state 当你Backspace时,自动删除尽可能多的空白和空行
-
C-c C-t 同时转换(开/关)auto-state和hungry-state子模式
-
C-c C-a 转换 auto-state 子模式
-
C-c C-d 转换 hungry-state 子模式
-
C-c . 设置缩进风格(按TAB键可列出可用的风格,缺省的为gnu,其缩进为2个字符;linux为8个;k&r为5个...)
-
TAB 重新缩进当前行
-
M-/ 自动补齐(缓冲区中能找得到的串)
-
M-; 行尾加入注释
-
C-c C-e 扩展宏
-
C-c C-c 注释掉整个区域
-
C-c C-" 将区域中的每一行结尾都加入一个'"'字符
编译和调试
启动gdb调试器后,光标在源码文件缓冲区中时:
-
C-x SPC 在当前行设置断点
-
C-x C-a C-s step
-
C-x C-a C-n next
-
C-x C-a C-t tbreak
-
C-x C-a C-r continue
GCJ Practice Contest Problem C:
In this problem, u r asked to calculate the number of all the Hamiltonian Circles in complete graph only with at most 15 forbidden edges.
We know, finding the Hamiltonian Circles is a NP problem, how can we count all the Hamiltonian Circles in a graph with so many edges?
First, let us think about this problem. Give u a complete graph with N vertexes , can u tell me how many Hamiltonian Circles exist in it?
We can easily find that number will be (N-1)! / 2.
And back to the original problem, we find that the forbidden edges are very small. So we come up with an idea, we can use the number of Hamiltonian Circles in a complete graph minus those circles using forbidden edges.

Code
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 9901;
const bool DB = false;
struct Edge
{
int s, t;
};
vector<int> edge[310];
Edge forb[16];
int N, K;
bool vis[310];
int fact[310];
int DFS(int now, int parent)
{
for (int i = 0; i < edge[now].size(); ++i)
if (!vis[ edge[now][i] ] && edge[now][i] != parent)
{
vis[ edge[now][i] ] = true;
return DFS(edge[now][i], now) + 1;
}
return 1;
}
int Solve()
{
memset(vis, false, sizeof(vis));
for (int i = 0; i < N; i++)
if (edge[i].size() > 2) return 0;
int comp = 0;
for (int i = 0; i < N; i++)
if (edge[i].size() == 1 && !vis[i])
{
vis[i] = true;
DFS(i, -1);
++comp;
}
int zeros = 0;
for (int i = 0; i < N; ++i)
if (edge[i].size() == 0)
++zeros;
for (int i = 0; i < N; ++i)
{
if (edge[i].size() == 2 && !vis[i])
{
int depth = 0;
// vis[i] = true;
depth = DFS(i, -1);
if (DB) printf("depth = %d\n", depth);
if (depth == N+1) return 1;
else
{
if (DB)
printf("exit when i = %d\n", i);
return 0;
}
}
}
if (DB)
printf("zeros = %d, comp = %d\n", zeros, comp);
int totn = zeros + comp;
if (comp) return fact[totn - 1] * (1LL << (comp-1)) % MOD;
else
return (fact[totn - 1] / 2) % MOD;
}
int main()
{
int t;
freopen("C-large-practice.in", "r", stdin);
freopen("C-large_parctice.out", "w", stdout);
fact[1] = 1;fact[0] = 1;
for (int i = 2; i <= 301; i++)
fact[i] = fact[i-1] * i % MOD;
scanf("%d", &t);
for (int ctr = 1; ctr <= t; ctr++)
{
scanf("%d%d", &N, &K);
for (int i = 0; i < K; i++)
{
scanf("%d%d", &forb[i].s, &forb[i].t);
forb[i].s--, forb[i].t--;
}
int ans = 0, ret;
for (int i = 0; i < (1<<K); i++)
{
if (DB)
printf("*****************\n");
for (int j = 0; j < N; j++)
edge[j].clear();
int mult = 1;
for (int j = 0; j < K; j++)
if (i&(1<<j))
edge[forb[j].s].push_back(forb[j].t), edge[forb[j].t].push_back(forb[j].s), mult *= -1;
ret = Solve();
if (DB)
printf("with mask %d, ret = %d, mult = %d\n", i, ret, mult);
ans = (ans + mult * ret) % MOD;
if (DB)
printf("ans = %d\n", ans);
}
printf("Case #%d: %d\n", ctr, ans < 0 ? ans + MOD : ans );
}
return 0;
}