您的位置:首页 > 产品设计 > UI/UE

UESTC第二届ACM趣味程序设计竞赛第三场

2010-12-25 22:15 477 查看
A. Painting
水题,容斥原理,小心又可能有0高度的。
B. WarCraft III
暴力C(24,5)。
C. Apple
有意思的题,问说n个数中有多少种方法,使得连续的数的和与0模m同余。
将前缀和按模m等价类划分。

代码

#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long int64;

const int MAX = 1000005;

int n, k;
int64 c[MAX];
int q[MAX];

int lowbit(int t)
{
return t & (-t);
}

void insert(int i)
{
while(i <= n){
c[i]++;
i += lowbit(i);
}
}

int64 query(int i)
{
int64 res = 0;
while(i > 0)
{
res += c[i];
i -= lowbit(i);
}
return res;
}

int main()
{
int T, cc = 0;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &k);
int h = 0, t = 0;
for(int i = n; i >= 1; i--)
{
q[t] = i;
t = (t + 1) % MAX;
for(int j = 0; j < k; j++)
{
q[t] = q[h];
h = (h + 1) % MAX;
t = (t + 1) % MAX;
}
}
int64 ans = 0;
memset(c, 0, sizeof(c));
while(h != t)
{
ans += query(q[h]);
insert(q[h]);
h = (h + 1) % MAX;
}
printf("Case #%d: %lld\n", ++cc, ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐