BestCoder Round #8 A,B,C

BestCoder Round #8

题目链接

A:签到题不多说

B:矩阵快速幂,奇数项的式子为f(n) = 4 * f(n - 1) + 1,偶数项是奇数项的两倍,然后构造矩阵为4 1 0 1进行快速幂即可

C:dp+树状数组加速,dp[i][j]表示以i为结尾长度为j的种数,然后把数字离散化掉,每次状态转移都需要从前一个区间和转移过来,所以可以利用树状数组维护

代码:

A:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;

typedef long long ll;
ll num[105];
set<ll> save;

int n;
int main() {
	while (~scanf("%d", &n)) {
		save.clear();
		for (int i = 0; i < n; i++) {
			scanf("%I64d", &num[i]);
			for (int j = 0; j < i; j++) {
				save.insert(num[i] + num[j]);
			}
		}
		ll ans = 0;
		for (set<ll>::iterator it = save.begin(); it != save.end(); it++) {
			ans += *it;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}

B:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;

typedef long long ll;

ll n, m;

struct mat {
	ll v[2][2];
	mat() {memset(v, 0, sizeof(v));}
	mat operator * (mat c) {
		mat ans;
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 2; j++) {
				for (int k = 0; k < 2; k++) {
					ans.v[i][j] = (ans.v[i][j] + v[i][k] * c.v[k][j]) % m;
				}
			}
		}
		return ans;
	}
};

mat pow_mod(mat A, ll k) {
	mat ans;
	for (int i = 0; i < 2; i++) ans.v[i][i] = 1;
	while (k) {
		if (k&1) ans = ans * A;
		A = A * A;
		k >>= 1;
	}
	return ans;
}

int main() {
	while (cin >> n >> m) {
		mat A;
		A.v[0][0] = 4; A.v[0][1] = 1;
		A.v[1][0] = 0; A.v[1][1] = 1;
		A = pow_mod(A, (n + 1) / 2);
		ll ans = A.v[0][1];
		ans %= m;
		if (n % 2 == 0) ans = ans * 2 % m;
		cout << ans << endl;
	}
	return 0;
}

C:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define lowbit(x) (x&(-x))

const int N = 10005;
const int M = 105;
const int MOD = 123456789;

int n, m, dp[N][M], bit[105][N];

void add(int *bit, int x, int v) {
	while (x < N) {
		bit[x] = (bit[x] + v) % MOD;
		x += lowbit(x);
	}
}

int get(int *bit, int x) {
	int ans = 0;
	while (x) {
		ans = (ans + bit[x]) % MOD;
		x -= lowbit(x);
	}
	return ans;
}

struct Num {
	int val, rank, id;
} num[N];

bool cmpid(Num a, Num b) {
	return a.id < b.id;
}

bool cmpval(Num a, Num b) {
	return a.val < b.val;
}

int main() {
	while (~scanf("%d%d", &n, &m)) {
		memset(bit, 0, sizeof(bit));
		for (int i = 1; i <= n; i++) {
			scanf("%d", &num[i].val);
			num[i].id = i;
		}
		sort(num + 1, num + n + 1, cmpval);
		num[1].rank = 2;
		for (int i = 2; i <= n; i++) {
			num[i].rank = num[i - 1].rank;
			if (num[i].val > num[i - 1].val)
				num[i].rank++;
		}
		sort(num + 1, num + n + 1, cmpid);
		add(bit[0], 1, 1);
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; i++) {
			for (int j = min(m, i); j >= 1; j--) {
				int tmp = get(bit[j - 1], num[i].rank - 1);
				dp[i][j] = (dp[i][j] + tmp) % MOD;
				add(bit[j], num[i].rank, tmp);
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; i++)
			ans = (ans + dp[i][m]) % MOD;
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-11-16 12:17:20

BestCoder Round #8 A,B,C的相关文章

BestCoder Round #91

传送门:http://acm.hdu.edu.cn/search.php?field=problem&key=BestCoder+Round+%2391&source=1&searchmode=source A题:给你n种字母,每种字母有个权值vali,共cnti个,现在让你在里面挑出任意数量的字符,组合成一个字符串,该字符串的权值的计算方式为val1*1+val2*2+--+valn*n,让你输出字符串最大的权值是多少.这题很容易会有一个错误的贪心,就是把val为负的舍去,然后v

BestCoder Round #65 (ZYB&#39;s Game)

ZYB's Game Accepts: 672 Submissions: 1207 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description ZYBZYBZYB played a game named NumberBombNumber BombNumberBomb with his classmates in hiking:a host keeps a

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)

Problem Description After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to

hdu 5163 Taking Bus (BestCoder Round #27)

Taking Bus                                                               Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 501    Accepted Submission(s): 203 Problem Description Bestland has a v

BestCoder Round #11 (Div. 2) 前三题题解

题目链接: huangjing hdu5054 Alice and Bob 思路: 就是(x,y)在两个參考系中的表示演全然一样.那么仅仅可能在这个矩形的中点.. 题目: Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 216    Accepted Submission(s): 166 Problem De

BestCoder Round #16

BestCoder Round #16 题目链接 这场挫掉了,3挂2,都是很sb的错误 23333 QAQ A:每个数字,左边个数乘上右边个数,就是可以组成的区间个数,然后乘的过程注意取模不然会爆掉 B:dp,dp[i][2]记录下第一长的LIS,和第二长的LIS,哎,转移的时候一个地方写挫掉了导致悲剧啊QAQ C:首先如果知道Nim游戏的,就很容易转化问题为,一些数字是否能选几个能否异或和为0,那么就是每个数字拆成40位,然后每一位异或和为0,这样就可以构造出40个方程,然后高斯消元求解,如果

BestCoder Round #11 (Div. 2) 题解

HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 302    Accepted Submission(s): 229 Problem Description Bob and Alice got separated in the Square, they agreed that if they

BestCoder Round #9

BestCoder Round #9 题目链接 A:暴力枚举一个数字,就能求出另一个数字,for一遍即可 B:博弈,判断前n - 1个开头连续1的奇偶性即可 C:先预处理出每个点对应哪几个点,每次查询计算一次即可 代码: A: #include <cstdio> #include <cstring> #include <vector> #include <set> #include <algorithm> #include <cmath&g

hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7    Accepted Submission(s): 4 Problem Description Hanamichi is taking part in

BestCoder Round #4 前两题 hdu 4931 4932

第一题太水了.. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int a[6]; 7 int main(){ 8 int cas; 9 scanf( "%d", &cas ); 10 while( cas-- ){ 11 for( int i = 0; i <