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. Dev环境中的集成测试用例执行时上下文环境检查(实战)

    Dev环境中的集成测试用例执行时上下文环境检查(实战) Microsoft.NET 解决方案,项目开发必知必会. 从这篇文章开始我将分享一系列我认为在实际工作中很有必要的一些.NET项目开发的核心技术 ...

  2. gof设计模式回顾

    gof23根据讲师学习笔记回顾: 1.gof:Gang of Four;叫grasp更具有针对性,解决具体的问题; ---------------------总共分为三大类: ---------创建型 ...

  3. 数据类型和typeof操作符

    虽然学习js有一段时间了,但是对js的基础语法却是有些生疏.最近在看jquery源码,决定随带总结一些基础的语法知识.今天总结一下数据类型和typeof,这在写js的时候,是不得不知道的知识. 数据类 ...

  4. .NET源码

    值得珍藏的.NET源码,不保存就没机会了 很早以前,我们通过http://referencesource.microsoft.com/netframework.aspx可以下载到.NET的各版本公开源 ...

  5. mac 下nginx加入开机启动

    通过brew install nginx后设置开机启动项 sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemonssudo launchc ...

  6. dmp文件导入的方法

    1,使用oracle的imp命令导入*.dmp文件 首先要明确知道,使用oracle的imp命令进行导入,要在一个空的数据库下,才是最好的,否则,数据表存在的话,就要先删除数据表中的数据 步骤1:禁用 ...

  7. WebForms vs. MVC

    [译]WebForms vs. MVC(推荐阅读) 正文如下======================================================= 原文示例(VS2012): ...

  8. ARC forbids explicit message send of 'autorelease'错误

    (ARC forbids explicit message send of 'autorelease'错误) 在ios中经常会遇到:ARC forbids explicit message send ...

  9. 简单2步实现 asp.net mvc ckeditor 图片上传

    1.打开ckeditor 包下的  config.js,添加一句 配置(PS:ckeditor 很多功能都在该配置文件里配置),如下: config.filebrowserImageUploadUrl ...

  10. Excel 菜单系统

    Excel 菜单系统 在开始Excel开发之前,需要把架子搭起来.最直接的那就是Excel里面的菜单了,他向用户直观的展现了我们的插件具有哪些功能.菜单出来之后我们就可以实现里面的事件和功能了.Exc ...