HDU ACM-Steps RECORD

Chapter 1

Section 1 暖手题

1.1.1 A+B for Input-Output Practice (I)

#include <stdio.h>
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)==2)
printf("%d\n",a+b);
return 0;
}

1.1.2 A+B for Input-Output Practice (II)

#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",a+b);
}
return 0;
}

1.1.3 A+B for Input-Output Practice (III)

#include <iostream>
using namespace std;
int main(){
int a, b;
while(cin >> a >> b && (a || b)) cout << a + b << endl;
return 0;
}

1.1.4 A+B for Input-Output Practice (IV)

#include <iostream>
using namespace std;
int main(){
int n, t;
while(cin >> n && n){
int ans = 0;
while(n--){
cin >> t;
ans += t;
}
cout << ans << endl;
}
return 0;
}

1.1.5 A+B for Input-Output Practice (V)

#include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int ans = 0;
while(n--){
int t;
cin >> t;
ans += t;
}
cout << ans << endl;
}
return 0;
}

1.1.6 A+B for Input-Output Practice (VI)

#include <iostream>
using namespace std;
int main(){
int n;
while(cin >> n){
int ans = 0;
while(n--){
int t;
cin >> t;
ans += t;
}
cout << ans << endl;
}
return 0;
}

1.1.7 A+B for Input-Output Practice (VII)

#include <iostream>
using namespace std;
int main(){
int a, b;
while(cin >> a >> b) cout << a + b << endl << endl;
return 0;
}

1.1.8 A+B for Input-Output Practice (VIII)

#include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
for(int i = 0; i < t; ++i){
if(i) cout << endl;
int n;
cin >> n;
int ans = 0;
while(n--){
int tmp;
cin >> tmp;
ans += tmp;
}
cout << ans << endl;
}
return 0;
}

Section 2 水题

1.2.1 Text Reverse

#include <stack>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
cin.get();
stack<char> s;
while(n--){
char ch;
while(true){
ch = cin.get();
if(ch == ' ' || ch == '\n'){
while(s.size()){
cout << s.top();
s.pop();
}
if(ch == '\n'){
cout << endl;
break;
}
else
cout << " ";
}
else
s.push(ch);
}
}
return 0;
}

1.2.2 find your present (2)

#include <iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false); // 取消同步 or 别用iostream or TLE
int n;
while(cin >> n && n){
int num, ans = 0;
while(n--){
cin >> num;
ans ^= num;
}
cout << ans << endl;
}
return 0;
}

1.2.3 Quicksum

#include <iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
string s;
while(getline(cin, s) && s != "#"){
int ans = 0;
for(int i = 1; i <= s.length(); ++i){
if(s[i-1] == ' ') continue;
ans += i * (s[i-1] - 'A' + 1);
}
cout << ans << endl;
}
return 0;
}

1.2.4 IBM Minus One

#include <string>
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 0; i < n; ++i){
string s;
cin >> s;
cout << "String #" << i + 1 << endl;
for(int i = 0; i < s.length(); ++i)
cout << char(s[i] == 'Z' ? 'A' : s[i] + 1);
cout << endl << endl;
}
return 0;
}

1.2.5 Lowest Bit

#include <iostream>
using namespace std;
int main(){
int n;
while(cin >> n && n)
cout << (n & (-n)) << endl; // 位运算求二进制最低位1(树状数组lowbit函数)
return 0;
}

1.2.6 ASCII

#include <iostream>
using namespace std;
int main(){
int n;
while(cin >> n){
int num;
while(n--){
cin >> num;
cout << char(num);
}
}
return 0;
}

1.2.7 Identity Card

#include <string>
#include <iostream>
using namespace std;
typedef long long ll;
int main(){
int n;
cin >> n;
while(n--){
string s;
cin >> s;
string region = "Shanghai";
if (s[0] == '3' && s[1] == '3')
region = "Zhejiang";
else if (s[0] == '1' && s[1] == '1')
region = "Beijing";
else if (s[0] == '7' && s[1] == '1')
region = "Taiwan";
else if (s[0] == '8' && s[1] == '1')
region = "Hong Kong";
else if (s[0] == '8' && s[1] == '2')
region = "Macao";
else if (s[0] == '5' && s[1] == '4')
region = "Tibet";
else if (s[0] == '2' && s[1] == '1')
region = "Liaoning";
cout << "He/She is from " << region << ",and his/her birthday is on "
<< s[10] << s[11] << ","
<< s[12] << s[13] << ","
<< s[6] << s[7] << s[8] << s[9]
<< " based on the table." << endl;
}
return 0;
}

1.2.8 AC Me

#include <map>
#include <string>
#include <iostream>
using namespace std;
typedef long long ll;
int main(){
string s;
while(getline(cin, s)){
map<char, int> ans;
for(int i = 0; i < s.length(); ++i) ++ans[s[i]];
for(int i = 'a'; i <= 'z'; ++i)
cout << char(i) << ":" << ans[char(i)] << endl;
cout << endl;
}
return 0;
}

Section 3 简单的贪心/模拟题

1.3.1 Moving Tables

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = 555;
int cnt[MAXN]; int main(){
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
memset(cnt, 0, sizeof(cnt));
int n;
cin >> n;
while(n--){
int s, e;
cin >> s >> e;
if(s > e) swap(s, e);
if(s & 1 == 0) --s;
if(e & 1 == 1) ++e;
for(int i = s; i <= e; ++i) cnt[i] += 10; // 统计每个房间被占用的时间
}
cout << *max_element(cnt, cnt+MAXN) << endl;
}
return 0;
}

1.3.2 今年暑假不AC

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; struct show{
int s, e;
bool operator < (show& other){
// 以 结束早,开始晚 为优先
return e != other.e ? e < other.e : s > other.s;
}
}; const int MAXN = 111;
show arr[MAXN]; int main(){
ios::sync_with_stdio(false);
int n;
while(cin >> n, n){
for(int i = 0; i < n; ++i) cin >> arr[i].s >> arr[i].e;
sort(arr, arr+n);
int ans = 0, flag = 0;
// 遍历统计不重叠区间数量
for(int i = 0; i < n; ++i){
if(arr[i].s >= flag){
++ans;
flag = arr[i].e;
}
}
cout << ans << endl;
}
return 0;
}

1.3.3 百步穿杨

#include <iostream>
#include <algorithm>
using namespace std; struct arrow{
int len, num;
bool operator < (arrow& other){
return len < other.len;
}
};
const int MAXN = 55;
arrow arr[MAXN]; int main(){
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
for(int i = 0; i < n; ++i) cin >> arr[i].len >> arr[i].num;
sort(arr, arr + n);
for(int i = 0; i < n; ++i){
int len = arr[i].len - 2, num = arr[i].num;
while(num--){
cout << ">+";
for(int k = 0; k < len; ++k) cout << "-";
cout << "+>" << endl;
}
cout << endl; // 每种箭后面需要一个空行
}
}
return 0;
}

1.3.4 What Is Your Grade?

好像写的有点乱......QAQ

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = 111; struct stu{
int num, index, score;
string time;
bool operator < (const stu& other){
return num != other.num ? num > other.num : time < other.time;
}
}; bool cmp(stu a, stu b){ return a.index < b.index; } stu arr[MAXN];
int cnt[MAXN]; int main(){
int n;
while(cin >> n, ~n){
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < n; ++i){
cin >> arr[i].num >> arr[i].time;
arr[i].index = i;
++cnt[arr[i].num];
}
sort(arr, arr + n);
int prank = cnt[arr[0].num];
for(int i = 0; i < n; ++i){
if(!prank) prank = cnt[arr[i].num];
arr[i].score = arr[i].num * 20 + (5 - arr[i].num) * 10;
if(arr[i].score == 50 || arr[i].score == 100){
--prank;
continue;
}
if(cnt[arr[i].num] == 1 || prank > int(ceil(cnt[arr[i].num] / 2.0))) arr[i].score += 5;
--prank;
}
sort(arr, arr + n, cmp);
for(int i = 0; i < n; ++i) printf("%d\n", arr[i].score);
puts("");
}
return 0;
}

1.3.5 第二小整数

#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = 11;
int arr[MAXN]; int main(){
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
for(int i = 0; i < n; ++i) cin >> arr[i];
sort(arr, arr + n);
cout << arr[1] << endl;
}
return 0;
}

1.3.6 考试排名

#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = 11111; struct info{
char name[22];
char status[15][11];
int solve, time;
bool operator < (const info& o){
return solve != o.solve ? solve > o.solve : time < o.time;
}
};
info arr[MAXN];
int problem, punish; int ifind(char* str, char ch){
int pos = 0, len = strlen(str);
for( ; pos < len; ++pos){
if(str[pos] == ch) return pos;
}
return pos;
} void calc(int index){
arr[index].solve = arr[index].time = 0;
info tmp = arr[index];
for(int i = 0; i < problem; ++i){
if(tmp.status[i][0] == '-' || tmp.status[i][0] == '0') continue;
++arr[index].solve;
int lpos = ifind(tmp.status[i], '(');
int tmptime = 0;
for(int j = 0; j < lpos; ++j) tmptime = tmptime * 10 + tmp.status[i][j] - '0';
arr[index].time += tmptime;
int rpos = ifind(tmp.status[i], ')');
if(rpos == strlen(tmp.status[i])) continue;
tmptime = 0;
for(int j = lpos + 1; j < rpos; ++j) tmptime = tmptime * 10 + tmp.status[i][j] - '0';
arr[index].time += (tmptime * punish);
}
} void read(int index, int n){
for(int i = 0; i < n; ++i) scanf("%s", arr[index].status[i]);
calc(index);
} int main(){
scanf("%d %d", &problem, &punish);
char* name; int index = 0;
while(~scanf("%s", arr[index].name)){
read(index, problem);
++index;
}
sort(arr, arr + index);
for(int i = 0; i < index; ++i) printf("%-10s %2d %4d\n", arr[i].name, arr[i].solve, arr[i].time);
return 0;
}

1.3.7 排列2

排序后用STL的next_permutation生成排列进行遍历检查

#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[4];
bool checkbegin = true; // 是否是第一组数据
while(cin >> arr[0] >> arr[1] >> arr[2] >> arr[3]){
if(!(arr[0] || arr[1] || arr[2] || arr[3])) break;
if(!checkbegin) cout << endl; // 两组数据×之间×空行
sort(arr, arr + 4);
while(!arr[0]) next_permutation(arr, arr + 4); // 去掉首位为0的数
int flag = arr[0]; // 记录前一个数的千位
bool checkfirst = true; // 是否是千位为flag的第一个数
do{
if(flag != arr[0]){
cout << endl; // 千位数不同,换行
flag = arr[0];
checkfirst = true;
}
if(!checkfirst) cout << " ";
for(int i = 0; i < 4; ++i) cout << arr[i];
checkfirst = false;
}while(next_permutation(arr, arr + 4));
cout << endl;
checkbegin = false;
}
return 0;
}

1.3.8 Crixalis's Equipment

#include <iostream>
#include <algorithm>
using namespace std; struct info{
int a, b;
bool operator < (const info& other){
int val1 = b - a, val2 = other.b - other.a;
// 按差值排序,尽量让剩余空间大,差值相同的以搬运所需空间大的为优先
return val1 == val2 ? b > other.b : val1 > val2;
}
};
info arr[1111];
int v, n; int main(){
int t;
cin >> t;
while(t--){
cin >> v >> n;
for(int i = 0; i < n; ++i) cin >> arr[i].a >> arr[i].b;
sort(arr, arr + n);
bool flag = true;
for(int i = 0; i < n; ++i){
if(arr[i].b > v){
flag = false;
break;
}
v -= arr[i].a;
}
cout << (flag ? "Yes" : "No") << endl;
}
return 0;
}

Chapter 2

Section 1

2.1.1 最小公倍数

#include <iostream>
#include <algorithm>
using namespace std; // int gcd(int a, int b){ reutrn b ? gcd(b, a % b) : a; } int main(){
int a, b;
while(cin >> a >> b) cout << a / __gcd(a, b) * b << endl; // algorithm内置的__gcd() O(∩_∩)O
return 0;
}

2.1.2 How many prime numbers

这题......数据好水...... 0.0

#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std; bool checkprime(int num){
int lmt = sqrt(num);
for(int i = 2; i <= lmt; ++i)
if(num % i == 0)
return false;
return true;
} int main(){
ios::sync_with_stdio(false);
int n;
while(cin >> n){
int ans = 0, tmp;
while(n--){
cin >> tmp;
if(checkprime(tmp)) ++ans;
}
cout << ans << endl;
}
return 0;
}

2.1.3 Cake

先分 \(k = gcd(a, b)\) 块,每份可以当做 \(\dfrac{a}{k}\) 块,也可以当做 \(\dfrac{b}{k}\) 块

按照切的刀数看,总共切了 \(a + b - k\) 刀,分成 \(a + b - k\) 块

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int a, b;
while(cin >> a >> b) cout << (a + b - __gcd(a, b)) << endl;
return 0;
}

2.1.4 又见GCD

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
int a, b;
cin >> a >> b;
a = a / b;
for(int i = 2; ; ++i)
if(__gcd(a, i) == 1){
cout << i * b << endl;
break;
}
}
return 0;
}

2.1.5 七夕节

#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int ans = 1;
int lmt = sqrt(n);
for(int i = 2; i <= lmt; ++i)
if(n % i == 0)
ans += (i + n / i);
if(n == lmt * lmt) ans -= lmt;
cout << ans << endl;
}
return 0;
}

2.1.6 整数对

将原来的数\(A\) 分为低位\(a\)、去掉的一位\(b\)、高位\(c\),设\(b\)为第\(k\)位,则有\(A = a + b * 10 ^ k + c * 10 ^ {k+1}\)

对于得到的新数字\(B\) 有:\(B = a + c * 10 ^ k\)

对于输入数据\(N\) 有:\(N = A + B = 2 * a + b * 10 ^ k + 11 * c * 10 ^ k\)

设 \(T = \dfrac{N}{10^k} = 11 * c + b\)

\(\dfrac{T}{11} = 11 * c \cdots\cdots b\)

则:

\(c = \dfrac{N}{11*10^k}\), \(b = \dfrac{N}{10^k} - 11 * c\), \(a = \dfrac{N - 11 * c * 10 ^ k - b * 10 ^ k}{2}\)

\(2 * a\) 可能会产生进位对 \(b\) 造成影响,但是对 \(a\) 无影响(b 最大为10,计算\(c\)时为除以11)

所以应对\(b\)分类讨论

#include <set>
#include <iostream>
using namespace std;
int main(){
int n;
while(cin >> n, n){
set<int> ans; // 去除重复 + 排序
int a, b, c;
for(int k = 1; k <= n; k *= 10){
c = n / 11 / k;
b = (n / k) - 11 * c;
if((b || c) && b < 10){ // 2*a 无进位
a = (n - 11 * c * k - b * k) >> 1;
if(2 * a + b * k + 11 * k * c == n) ans.insert(a + b * k + c * k * 10);
}
--b;
if((b || c) && b >= 0){ // 2*a 进位
a = (n - 11 * c * k - b * k) >> 1;
if(2 * a + b * k + 11 * k * c == n) ans.insert(a + b * k + c * k * 10);
}
}
if(ans.empty()) cout << "No solution." << endl;
else{
for(set<int>::iterator it = ans.begin(); it != ans.end(); ++it){
if(it != ans.begin()) cout << " ";
cout << *it;
}
cout << endl;
}
}
return 0;
}

2.1.7 The area

P1(x1, y1) P2(x2, y2) P3(x3, y3)

设直线 \(y_l = kx + h\), 抛物线 \(y_p = ax^2+bx+c\)

对于\(y_l\)有 \(k = \dfrac{y_2-y_3}{x_2-x_3}\), \(h = y_2 - k * x_2\)

对于\(y_p\): \(x_1 = -\dfrac{b}{2a}\), \(y_1 = \dfrac{4ac-b^2}{4a}\)

则: \(b = -2ax_1\)

又有: \(a{x_1}^2 + bx_1 + c = y_1\), \(a{x_2}^2 + bx_2 + c = y_2\)

\(\therefore a({x_2}^2 - {x_1}^2) - 2ax_1(x_2-x_1) = y_2-y_1\)

\(a = \dfrac{y_2-y_1}{{(x_2-x_1)}^2}\), \(b = -2ax_1\), \(c = y_1 - a{x_1}^2 - bx_1\)

设面积为\(S\)

\(y_p - y_l = ax^2 + (b-k)x + (c-h)\)

\(\displaystyle{S = \int_{x_2}^{x_3}(y_p - y_l) \, dx = \int_{x_2}^{x_3}(ax^2 + (b-k)x + (c-h)) \, dx}\)

求个定积分 \(\left(\left(\dfrac{a}{3}\right)({x_3}^3-{x_2}^3)+\left(\dfrac{b-k}{2}\right)({x_3}^2-{x_2}^2)+(c-h)(x_3-x_2)\right)\Bigg|_{x_2}^{x_3}\)

#include <cstdio>

int t;
double x1, y1, x2, y2, x3, y3; int main(){
scanf("%d", &t);
while(t--){
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
double k = (y2-y3)/(x2-x3), h = y2-k*x2, a = (y2-y1)/(x2-x1)/(x2-x1), b = -2*a*x1, c = y1-a*x1*x1-b*x1;
printf("%.2f\n", ((a/3)*(x3*x3*x3-x2*x2*x2)+((b-k)/2)*(x3*x3-x2*x2)+(c-h)*(x3-x2)));
}
return 0;
}

2.1.8 Leftmost Digit

typedef long long ll;

科学计数法表示: \(n^n = a * 10^x\)

等号两边取对数: \(n*lg(n) = x + \lg(a)\) 由于\(0\le \lg(n) \le 1\),所以\(x\)为\(n*lg(n)\)的整数部分

\(\lg(a) = n*\lg(n) - x\)

\(\lg(a) = n*\lg(n) - ll(n*\lg(n))\)

\(a = 10^{n*\lg(n) - ll(n*\lg(n))}\)

#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
ll n;
cin >> n;
cout << ll(pow(10, (n*log10(n) - ll(n*log10(n))))) << endl;
}
return 0;
}

Section 2

2.2.1 Fibonacci

Fibonacci公式: \(\displaystyle{F_n = \dfrac{1}{\sqrt5}\left[{\left(\dfrac{1+\sqrt5}{2}\right)}^n-{\left(\dfrac{1-\sqrt5}{2}\right)}^n\right]}\)

按照[Leftmost Digit](#Leftmost Digit)的方法求解

直接求\(F_n\)不太科学,考虑到用的是\(F_n\)的对数,对等号两边取对数,化简:

\(\lg(F_n) = \lg\left(\dfrac{1}{\sqrt5}\right) + n\,\lg\left(\dfrac{1 + \sqrt5}{2}\right) + \lg\left[1-{\left(\dfrac{1-\sqrt5}{1+\sqrt5}\right)}^n\right]\)

#include <math.h>
#include <stdio.h>
int main(){
int n;
int fib[233] = {0, 1};
for(int i = 2; i < 21; ++i) fib[i] = (fib[i-1] + fib[i-2]) % 10000;
while(~scanf("%d", &n)){
if(n < 21) {
printf("%d\n", fib[n]);
continue;
}
double logFn = log10((1/sqrt(5))) + n*log10((1+sqrt(5))/2) + log10(1-pow((1-sqrt(5))/(1+sqrt(5)),n));
double a = pow(10, logFn - int(logFn));
printf("%d\n", int(a * 1000));
}
return 0;
}

2.2.2 Fibonacci

相当于每次将ban掉的人去掉

#include <iostream>
using namespace std; int main(){
ios::sync_with_stdio(false);
int arr[15];
for(int i = 1; i < 15; ++i)
for(int ans = i+1; ; ++ans){
int p = 0, left;
bool flag = true;
for(left = 2*i; left > i; --left){
// 将从1开头的编号对应到从0开头的编号以满足取模的需求
p = (p + ans - 1) % left; // 不管ban了多少人,只要每次求出来的p比i小就代表杀死好人
if(p < i){ flag = false; break; }
}
if(flag && left == i){ arr[i] = ans; break; }
}
int n;
while(cin >> n, n) cout << arr[n] << endl;
return 0;
}

2.2.3

类似于递归检查是否满足条件

如果起始柱或目标柱上盘子数等于当前检查的盘子总数则是满足条件

否则,检查最大的盘子,如果在借助柱则不满足条件,如果不在借助柱则检查当前盘子总数-1的情况

#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 66;
int t, N, n[3], id[3][MAXN], pos[3];
int main(){
ios::sync_with_stdio(false);
cin >> t;
while(t--){
int aa = 0, bb = 1, cc = 2;
pos[aa] = pos[bb] = pos[cc] = 0;
cin >> N;
cin >> n[0]; for(int i = 0; i < n[0]; ++i) cin >> id[aa][i];
cin >> n[1]; for(int i = 0; i < n[1]; ++i) cin >> id[bb][i];
cin >> n[2]; for(int i = 0; i < n[2]; ++i) cin >> id[cc][i];
bool flag = true;
while(true){
if(n[aa] == N || n[cc] == N){ flag = true; break; }
if(n[bb] && id[bb][pos[bb]] == N){ flag = false; break; }
if(n[aa] && id[aa][pos[aa]] == N){
--N;
--n[aa];
++pos[aa];
swap(bb, cc);
continue;
}
if(n[cc] && id[cc][pos[cc]] == N){
--N;
--n[cc];
++pos[cc];
swap(aa, bb);
continue;
}
}
cout << (flag ? "true" : "false") << endl;
}
return 0;
}

2.2.4 Wolf and Rabbit

n, m最大公约数为1时代表wolf能从hole0走到hole1,也就能走到其他所有的hole

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int t, n, m;
cin >> t;
while(t--){
cin >> n >> m;
cout << (__gcd(n, m) == 1 ? "NO" : "YES") << endl;
}
return 0;
}

2.2.5 Number Sequence

#include <iostream>
#include <algorithm>
using namespace std; int main(){
ios::sync_with_stdio(false);
int a, b, n;
while(cin >> a >> b >> n, a || b || n){
int i, arr[55] = {0, 1, 1};
for(i = 3; i < 50; ++i){
arr[i] = (a * arr[i-1] + b * arr[i-2]) % 7;
if(arr[i] == 1 && arr[i-1] == 1) break; // 找循环节,最多不会超过50
}
n %= (i - 2);
cout << (n ? arr[n] : arr[i-2]) << endl;
}
return 0;
}

2.2.6 Train Problem II

Catalan公式: \(Catalan_{n} = \dfrac{C_{2n}^n}{n + 1}\)

Catalan递推公式: \(Catalan_{n} = \dfrac{(4n-2)Catalan_{n-1}}{n+1}\)

import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
BigInteger[] ans = new BigInteger[111];
ans[0] = ans[1] = new BigInteger("1");
for(int i = 2; i < 111; ++i)
ans[i] = ans[i-1].multiply(new BigInteger(Integer.toString(4*i-2))).divide(new BigInteger(Integer.toString(i+1)));
while(scan.hasNext()){
int n = scan.nextInt();
System.out.println(ans[n]);
}
scan.close();
}
}

```cpp


[2.2.7]()
```cpp

2.2.8 Big Number

对于一个数k,求位数:\(\lg(k) + 1\)

\(1 \le n \le 10^7\)范围内可以直接用循环求: \(\lg(n!) = \lg(1) + \lg(2) + \cdots + \lg(n)\)

对于大数阶乘的值\(n!\)还可以用斯特林公式: \(n! \approx \sqrt{2{\pi}n}\left(\dfrac{n}{e}\right)^n\) 即使在n较小时取值也非常准确

则: \(\lg(n!) = \lg\left(\sqrt{2{\pi}n}\left(\dfrac{n}{e}\right)^n\right) = \dfrac{1}{2}\lg(2{\pi}n) + n\lg\left(\dfrac{n}{e}\right)\)

#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1.0);
const double e = exp(1.0); int main(){
ios::sync_with_stdio(false); // 直接运算
//int t, n;
//cin >> t;
//while(t--){
// cin >> n;
// double ans = 0.0;
// for(int i = 1; i <= n; ++i) ans += log10(i);
// cout << int(ans) + 1 << endl;
//} // 运用斯特林公式运算
int t, n;
cin >> t;
while(t--){
cin >> n;
cout << int(0.5*log10(2*pi*n)+n*log10(n/e)+1) << endl;
}
}

HDU ACM-Steps的更多相关文章

  1. hdu acm steps Big Event in HDU

    上网搜了一下这道题的解法,主要有两个方法,一种是采用母函数的方法,一种是采用0/1背包的方法. 先说一下母函数,即生成函数,做个比喻,母函数就是一个多项式前面的系数的一个整体的集合,而子函数就是这个多 ...

  2. hdu ACM Steps Section 1 花式A+B 输入输出格式

    acm与oi很大的一个不同就是在输入格式上.oi往往是单组数据,而acm往往是多组数据,而且题目对数据格式往往各有要求,这8道a+b(吐槽..)涉及到了大量的常用的输入输出格式.https://wen ...

  3. hdu acm 1028 数字拆分Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  4. HDOJ acm steps 3.1.1

    (都是递推求值,呵呵,好开心- - ) 今天又是在自习室通宵(文明玩的停不下来了) 游戏玩完想想该水题了,于是打开了HDOJ的ACM STEPS(这是个好东西,就像他的名字,一步步来的) 2.3.x貌 ...

  5. hdu acm 1166 敌兵布阵 (线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. hdu acm 2082 找单词

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. HDU ACM 1325 / POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU ACM 1134 Game of Connections / 1130 How Many Trees?(卡特兰数)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1134 [解题背景]这题不会做,自己推公式推了一段时间,将n=3和n=4的情况列出来了,只发现第n项与 ...

  9. HDU ACM Eight

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 解题背景: 看到八数码问题,没有任何的想法,偶然在翻看以前做的题的时候发现解决过类似的一道题,不 ...

  10. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

随机推荐

  1. Qlik Sense Desktop

    2014年新一代的报表利器 Qlik Sense Desktop 初步体验 2014-09-22 12:45 by BIWORK, 974 阅读, 7 评论, 收藏, 编辑 Qlik Sense De ...

  2. 删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005,

    原文:删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005, --删除指定表中所有索引 --用法:declare @tableName varchar(100 ...

  3. 自己写RTPserver——大约RTP协议

    自己写RTPserver--大约RTP协议 本文将带领你一步一步地实现一个简单的手RTP变速器server,旨在了解RTP流媒体传输协议以及有关多媒体编解码器的一些知识. RTP协议的必备知识 要动手 ...

  4. 查看mysql状态常用命令

    最近服务器上mysql有些奇奇怪怪的问题,可惜我不是专业的dba,为了加深自己对mysql的了解,先从基础的查看mysql状态命令看起吧. 命令: show status; 命令: show stat ...

  5. {{angular.js 使用技巧}} - 实现计算列属性

    前端MV*框架现在有很多,其中某些框架有计算列(又叫监控属性),比如:微软推荐的 Knockout.js 和博客园司徒正美的 avalon.js 框架. 本人只使用过 Knockout.js,aval ...

  6. Idea安装GO语言插件

    https://github.com/go-lang-plugin-org/go-lang-idea-plugin 安装方法写的很清楚,网上也很多我就不细写了,只是有一个问题,下载插件下不下来,懒得找 ...

  7. FineUI开源版之TreeGrid(修改)

    上篇文章中做了简单实现,但是还是有bug的,还需要在外面写事件的处理,今天又进行修改了. 下面放出代码,同样的  hzh modify标记的就是我进行修改的地方 grid.cs 添加代码 #regio ...

  8. Hadoop能力测试图谱

    一张图测试你的Hadoop能力-Hadoop能力测试图谱 1.引言 看到一张图,关于Hadoop技术框架的图,基本上涉及到Hadoop当前应用的主要领域,感觉可以作为测试Hadoop开发人员当前能力和 ...

  9. dtrace sample

    #!/usr/sbin/dtrace -s #pragma D option flowindent /* monitor file open */ syscall::open:entry { prin ...

  10. 对 Linux 新手有用的 20 个命令

    你打算从Windows换到Linux上来,还是你刚好换到Linux上来?哎哟!!!我说什么呢,是什么原因你就出现在我的世界里了.从我以往的经验来说,当我刚使用Linux,命令,终端啊什么的,吓了我一跳 ...