ACM-BestCoder Round #33
来源:互联网 发布:汽车英文翻译软件 编辑:程序博客网 时间:2024/06/14 14:18
A-zhx's submissions
题意:
给出n个b进制的数据,要求计算它们的和,但是不需要进位。
分析:
反转每一个数据后,模拟每一位的加法即可,需要注意的地方主要有去掉前导零,但是当只有一个零时却要输出零。具体做法的话,可以将每一位转化成数字,累加到数组中,因为不需要进位,最后统一取模即可。
源代码:
// 用cstdio,交g++的话会超时#include <stdio.h>#include <cstring>#include <algorithm>#include <ctype.h>using namespace std;#define MAX 205int sum[MAX];char data[MAX];int n, b;void add(){ int ma = 0; memset(sum, 0, sizeof(sum)); while(n--) { scanf("%s", data); int len = strlen(data); // 反转,对齐个位 reverse(data, data+len); for(int i=0; i<len; ++i) { if(isdigit(data[i])) { sum[i] += data[i]-'0'; } else { sum[i] += data[i]-'a'+10; } } ma = max(ma, len); } // 先取模,因为这一步可能会产生前导零 for(int i=0; i<ma; ++i) { sum[i] %= b; } // 去掉前导零,但需要留一个 while(ma>1 && sum[ma-1]==0) { --ma; } for(int i=ma-1; i>=0; --i) { if(sum[i] < 10) { printf("%d", sum[i]); } else { printf("%c", 'a'+(sum[i]-10)); } } puts("");}int main(){ while(~scanf("%d%d", &n, &b)) { add(); } return 0;}
B-zhx's contest
题意:
找出数列1,2,3,...,n的全排列中漂亮排列的个数,漂亮排列即可以在该排列中找出一项ai,使得数列{a1,..,ai}和数列{ai,...,an}都是单调递增或单调递减的。
分析:
若ai所在数列是漂亮数列,那么ai要么最大要么最小,因为ai是单调的分界,只有这两种情况,所以满足这个条件的情况共有2^n种,而整个数列的单调递增和单调递减的情况被算了两次,所以需要减2,那么最终的答案就是2^n-2,但是注意当n=1时,答案为1。但是注意n的范围,太大了不能直接计算,所以考虑使用快速幂取模。
源代码:
#include <cstdio>#include <cstring>#define LL __int64LL n, p;LL FaMulti(LL a, LL b){ LL res = 0; while(b) { if(b & 1) { res += a; res %= p; } a += a; a %= p; b >>= 1; } return res;}LL FaPow(LL a, LL b){ LL res = 1; while(b) { if(b & 1) { res = FaMulti(res, a); } a = FaMulti(a, a); b >>= 1; } return res;}int main(){ while(~scanf("%I64d%I64d", &n, &p)) { if(n == 1) { // 注意这里也需要取模 printf("%I64d\n", 1%p); } else { LL ans = FaPow(2,n) - 2; // 由于减了2,所以结果可能为负,需要加上模 while(ans < 0) { ans += p; } printf("%I64d\n", ans); } } return 0;}
0 0
- ACM-BestCoder Round #33
- ACM-BestCoder Round #19
- ACM-BestCoder Round #20
- ACM-BestCoder Round #21
- ACM-BestCoder Round #22
- ACM-BestCoder Round #32
- ACM-BestCoder Round #31
- BestCoder Round #33
- BestCoder-Round#33
- BestCoder Round #33
- BestCoder Round #33 C
- Bestcoder round#33 解题报告
- BestCoder Round #33 1001 zhx's submissions
- BestCoder Round #33 1002 zhx's contest
- BestCoder Round #3 BestCoder Sequence
- bestcoder round #1
- BestCoder round #1
- BestCoder Round #1
- Myeclipse 2014的ssh整合出错java.lang.NoSuchMethodError: antlr.collections.AST.getLine()
- springmvc的特性
- 算法实现-->最长子序列问题
- 实现操作符重载的两种方式
- ImageView.ScaleType
- ACM-BestCoder Round #33
- const 用法总结
- 代码备忘, TODO宏实现
- 优先队列priority_queue 用法详解
- 第二周项目二--长方柱类
- 杭电ACM1010——深度优先搜索+奇偶剪枝
- 欢迎使用CSDN-markdown编辑器
- HDU1.2.3解题报告
- fgets