- copy
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
- merge (如果相等,第一个优先)
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
- for_each
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);
- transform
template <class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperation op);
- numeric_limits
template <class T> numeric_limits;
- iota
template< class ForwardIterator, class T >
void iota( ForwardIterator first, ForwardIterator last, T value );
// Routines for performing computations on dates. In these routines,
// months are exprsesed as integers from 1 to 12, days are expressed
// as integers from 1 to 31, and years are expressed as 4-digit
// integers.
string dayOfWeek[] = {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"};
// converts Gregorian date to integer (Julian day number)
int DateToInt (int m, int d, int y){
return
1461 * (y + 4800 + (m - 14) / 12) / 4 +
367 * (m - 2 - (m - 14) / 12 * 12) / 12 -
3 * ((y + 4900 + (m - 14) / 12) / 100) / 4 +
d - 32075;
}
// converts integer (Julian day number) to Gregorian date: month/day/year
void IntToDate (int jd, int &m, int &d, int &y){
int x, n, i, j;
x = jd + 68569;
n = 4 * x / 146097;
x -= (146097 * n + 3) / 4;
i = (4000 * (x + 1)) / 1461001;
x -= 1461 * i / 4 - 31;
j = 80 * x / 2447;
d = x - 2447 * j / 80;
x = j / 11;
m = j + 2 - 12 * x;
y = 100 * (n - 49) + i + x;
}
// converts integer (Julian day number) to day of week
string IntToDay (int jd){
return dayOfWeek[jd % 7];
}
- 枚举真子集
for (int s = (S - 1) & S; s; s = (s - 1) & S)
- 枚举大小为 k 的子集
template<typename T>
void subset(int k, int n, T&& f) {
int t = (1 << k) - 1;
while (t < 1 << n) {
f(t);
int x = t & -t, y = t + x;
t = ((t & ~y) / x >> 1) | y;
}
}
LL dfs(LL base, LL pos, LL len, LL s, bool limit) {
if (pos == -1) return s ? base : 1;
if (!limit && dp[base][pos][len][s] != -1) return dp[base][pos][len][s];
LL ret = 0;
LL ed = limit ? a[pos] : base - 1;
FOR (i, 0, ed + 1) {
tmp[pos] = i;
if (len == pos)
ret += dfs(base, pos - 1, len - (i == 0), s, limit && i == a[pos]);
else if (s &&pos < (len + 1) / 2)
ret += dfs(base, pos - 1, len, tmp[len - pos] == i, limit && i == a[pos]);
else
ret += dfs(base, pos - 1, len, s, limit && i == a[pos]);
}
if (!limit) dp[base][pos][len][s] = ret;
return ret;
}
LL solve(LL x, LL base) {
LL sz = 0;
while (x) {
a[sz++] = x % base;
x /= base;
}
return dfs(base, sz - 1, sz - 1, 1, true);
}
- 最小覆盖圆
using LD = double;
const int N = 1E4 + 100;
int x[N], y[N], n;
LD eval(LD xx, LD yy) {
LD r = 0;
FOR (i, 0, n)
r = max(r, sqrt(pow(xx - x[i], 2) + pow(yy - y[i], 2)));
return r;
}
mt19937 mt(time(0));
auto rd = bind(uniform_real_distribution<LD>(-1, 1), mt);
int main() {
int X, Y;
while (cin >> X >> Y >> n) {
FOR (i, 0, n) scanf("%d%d", &x[i], &y[i]);
pair<LD, LD> ans;
LD M = 1e9;
FOR (_, 0, 100) {
LD cur_x = X / 2.0, cur_y = Y / 2.0, T = max(X, Y);
while (T > 1e-3) {
LD best_ans = eval(cur_x, cur_y);
LD best_x = cur_x, best_y = cur_y;
FOR (___, 0, 20) {
LD nxt_x = cur_x + rd() * T, nxt_y = cur_y + rd() * T;
LD nxt_ans = eval(nxt_x, nxt_y);
if (nxt_ans < best_ans) {
best_x = nxt_x; best_y = nxt_y;
best_ans = nxt_ans;
}
}
cur_x = best_x; cur_y = best_y;
T *= .9;
}
if (eval(cur_x, cur_y) < M) {
ans = {cur_x, cur_y}; M = eval(cur_x, cur_y);
}
}
printf("(%.1f,%.1f).\n%.1f\n", ans.first, ans.second, eval(ans.first, ans.second));
}
}
- 可以用
auto p = reinterpret_cast<unsigned*>(&x);
(p[0]
的最低位就是bitset
的最低位)
// M 要开大至少 1 个 64
const int M = (1E4 + 200) / 64;
typedef unsigned long long ULL;
const ULL ONE = 1;
struct Bitset {
ULL a[M];
void go(int x) {
int offset = x / 64; x %= 64;
for (int i = offset, j = 0; i + 1 < M; ++i, ++j) {
a[j] |= a[i] >> x;
if (x) a[j] |= a[i + 1] << (64 - x); // 不能左移 64 位
}
}
void init() { memset(a, 0, sizeof a); }
void set(int x) {
int offset = x / 64; x %= 64;
a[offset] |= (ONE << x);
}
void prt() {
FOR (i, 0, M) FOR (j, 0, 64) putchar((a[i] & (ONE << j)) ? '1' : '0');
puts("");
}
int lowbit() {
FOR (i, 0, M) if (a[i]) return i * 64 + __builtin_ctzll(a[i]);
assert (0);
}
int highbit(int x) {
// [0,x) 的最高位
int offset = x / 64; x %= 64;
FORD (i, offset, -1) {
if (!a[i]) continue;
if (i == offset) {
FORD (j, x - 1, -1) if ((ONE << j) & a[i]) { return i * 64 + j; }
} else return i * 64 + 63 - __builtin_clzll(a[i]);
}
assert (0);
}
};
- 不要使用
rand()
。 chrono::steady_clock::now().time_since_epoch().count()
可用于计时。- 64 位可以使用
mt19937_64
。
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<int> permutation(N);
for (int i = 0; i < N; i++)
permutation[i] = i;
shuffle(permutation.begin(), permutation.end(), rng);
for (int i = 0; i < N; i++)
permutation[i] = i;
for (int i = 1; i < N; i++)
swap(permutation[i], permutation[uniform_int_distribution<int>(0, i)(rng)]);
unsigned rnd() {
static unsigned A = 1 << 16 | 3, B = 33333331, C = 2341;
return C = A * C + B;
}
mt19937 mt(time(0));
auto rd = bind(uniform_real_distribution<double>(0, 1), mt);
auto rd2 = bind(uniform_int_distribution<int>(1, 6), mt);
42737, 46411, 50101, 52627, 54577, 191677, 194869, 210407, 221831, 241337, 578603, 625409, 713569, 788813, 862481, 2174729, 2326673, 2688877, 2779417, 3133583, 4489747, 6697841, 6791471, 6878533, 7883129, 9124553, 10415371, 11134633, 12214801, 15589333, 17148757, 17997457, 20278487, 27256133, 28678757, 38206199, 41337119, 47422547, 48543479, 52834961, 76993291, 85852231, 95217823, 108755593, 132972461, 171863609, 173629837, 176939899, 207808351, 227218703, 306112619, 311809637, 322711981, 330806107, 345593317, 345887293, 362838523, 373523729, 394207349, 409580177, 437359931, 483577261, 490845269, 512059357, 534387017, 698987533, 764016151, 906097321, 914067307, 954169327
1572869, 3145739, 6291469, 12582917, 25165843, 50331653 (适合哈希的素数)
from random import randint
def is_prime(num, test_count):
if num == 1:
return False
if test_count >= num:
test_count = num - 1
for x in range(test_count):
val = randint(1, num - 1)
if pow(val, num-1, num) != 1:
return False
return True
def generate_big_prime(n):
found_prime = False
while not found_prime:
p = randint(2**(n-1), 2**n)
if is_prime(p, 1000):
return p
3, 1, 1, 2; 5, 1, 2, 2; 17, 1, 4, 3; 97, 3, 5, 5; 193, 3, 6, 5; 257, 1, 8, 3; 7681, 15, 9, 17; 12289, 3, 12, 11; 40961, 5, 13, 3; 65537, 1, 16, 3; 786433, 3, 18, 10; 5767169, 11, 19, 3; 7340033, 7, 20, 3; 23068673, 11, 21, 3; 104857601, 25, 22, 3; 167772161, 5, 25, 3; 469762049, 7, 26, 3; 1004535809, 479, 21, 3; 2013265921, 15, 27, 31; 2281701377, 17, 27, 3; 3221225473, 3, 30, 5; 75161927681, 35, 31, 3; 77309411329, 9, 33, 7; 206158430209, 3, 36, 22; 2061584302081, 15, 37, 7; 2748779069441, 5, 39, 3; 6597069766657, 3, 41, 5; 39582418599937, 9, 42, 5; 79164837199873, 9, 43, 5; 263882790666241, 15, 44, 7; 1231453023109121, 35, 45, 3; 1337006139375617, 19, 46, 3; 3799912185593857, 27, 47, 5; 4222124650659841, 15, 48, 19; 7881299347898369, 7, 50, 6; 31525197391593473, 7, 52, 3; 180143985094819841, 5, 55, 6; 1945555039024054273, 27, 56, 5; 4179340454199820289, 29, 57, 3.
// Code which demonstrates the use of Java's regular expression libraries.
// This is a solution for
//
// Loglan: a logical language
// http://acm.uva.es/p/v1/134.html
import java.util.*;
import java.util.regex.*;
public class LogLan {
public static void main(String args[]) {
String regex = BuildRegex();
Pattern pattern = Pattern.compile(regex);
Scanner s = new Scanner(System.in);
while (true) {
// In this problem, each sentence consists of multiple lines, where the last
// line is terminated by a period. The code below reads lines until
// encountering a line whose final character is a '.'. Note the use of
//
// s.length() to get length of string
// s.charAt() to extract characters from a Java string
// s.trim() to remove whitespace from the beginning and end of Java string
//
// Other useful String manipulation methods include
//
// s.compareTo(t) < 0 if s < t, lexicographically
// s.indexOf("apple") returns index of first occurrence of "apple" in s
// s.lastIndexOf("apple") returns index of last occurrence of "apple" in s
// s.replace(c,d) replaces occurrences of character c with d
// s.startsWith("apple) returns (s.indexOf("apple") == 0)
// s.toLowerCase() / s.toUpperCase() returns a new lower/uppercased string
//
// Integer.parseInt(s) converts s to an integer (32-bit)
// Long.parseLong(s) converts s to a long (64-bit)
// Double.parseDouble(s) converts s to a double
String sentence = "";
while (true) {
sentence = (sentence + " " + s.nextLine()).trim();
if (sentence.equals("#")) return;
if (sentence.charAt(sentence.length() - 1) == '.') break;
}
// now, we remove the period, and match the regular expression
String removed_period = sentence.substring(0, sentence.length() - 1).trim();
if (pattern.matcher(removed_period).find()) {
System.out.println("Good");
} else {
System.out.println("Bad!");
}
}
}
}
// examples for printing floating point numbers
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class DecFormat {
public static void main(String[] args) {
DecimalFormat fmt;
// round to at most 2 digits, leave of digits if not needed
fmt = new DecimalFormat("#.##");
System.out.println(fmt.format(12345.6789)); // produces 12345.68
System.out.println(fmt.format(12345.0)); // produces 12345
System.out.println(fmt.format(0.0)); // produces 0
System.out.println(fmt.format(0.01)); // produces .1
// round to precisely 2 digits
fmt = new DecimalFormat("#.00");
System.out.println(fmt.format(12345.6789)); // produces 12345.68
System.out.println(fmt.format(12345.0)); // produces 12345.00
System.out.println(fmt.format(0.0)); // produces .00
// round to precisely 2 digits, force leading zero
fmt = new DecimalFormat("0.00");
System.out.println(fmt.format(12345.6789)); // produces 12345.68
System.out.println(fmt.format(12345.0)); // produces 12345.00
System.out.println(fmt.format(0.0)); // produces 0.00
// round to precisely 2 digits, force leading zeros
fmt = new DecimalFormat("000000000.00");
System.out.println(fmt.format(12345.6789)); // produces 000012345.68
System.out.println(fmt.format(12345.0)); // produces 000012345.00
System.out.println(fmt.format(0.0)); // produces 000000000.00
// force leading '+'
fmt = new DecimalFormat("+0;-0");
System.out.println(fmt.format(12345.6789)); // produces +12346
System.out.println(fmt.format(-12345.6789)); // produces -12346
System.out.println(fmt.format(0)); // produces +0
// force leading positive/negative, pad to 2
fmt = new DecimalFormat("positive 00;negative 0");
System.out.println(fmt.format(1)); // produces "positive 01"
System.out.println(fmt.format(-1)); // produces "negative 01"
// qoute special chars (#)
fmt = new DecimalFormat("text with '#' followed by #");
System.out.println(fmt.format(12.34)); // produces "text with # followed by 12"
// always show "."
fmt = new DecimalFormat("#.#");
fmt.setDecimalSeparatorAlwaysShown(true);
System.out.println(fmt.format(12.34)); // produces "12.3"
System.out.println(fmt.format(12)); // produces "12."
System.out.println(fmt.format(0.34)); // produces "0.3"
// different grouping distances:
fmt = new DecimalFormat("#,####.###");
System.out.println(fmt.format(123456789.123)); // produces "1,2345,6789.123"
// scientific:
fmt = new DecimalFormat("0.000E00");
System.out.println(fmt.format(123456789.123)); // produces "1.235E08"
System.out.println(fmt.format(-0.000234)); // produces "-2.34E-04"
// using variable number of digits:
fmt = new DecimalFormat("0");
System.out.println(fmt.format(123.123)); // produces "123"
fmt.setMinimumFractionDigits(8);
System.out.println(fmt.format(123.123)); // produces "123.12300000"
fmt.setMaximumFractionDigits(0);
System.out.println(fmt.format(123.123)); // produces "123"
// note: to pad with spaces, you need to do it yourself:
// String out = fmt.format(...)
// while (out.length() < targlength) out = " "+out;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Employee implements Comparable<Employee> {
private int id;
private String name;
private int age;
public Employee(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public int compareTo(Employee o) {
if (id > o.id) {
return 1;
} else if (id < o.id) {
return -1;
}
return 0;
}
public static void main(String[] args) {
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(2, "Java", 20));
list.add(new Employee(1, "C", 30));
list.add(new Employee(3, "C#", 10));
Collections.sort(list);
}
}
#include <sys/resource.h>
void init_stack(){
const rlim_t kStackSize = 512 * 1024 * 1024;
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0) {
if (rl.rlim_cur < kStackSize) {
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0) {
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
}
(int)v.size()
1LL << k
- 递归函数用全局或者 static 变量要小心
- 预处理组合数注意上限
- 想清楚到底是要
multiset
还是set
- 提交之前看一下数据范围,测一下边界
- 数据结构注意数组大小 (2倍,4倍)
- 字符串注意字符集
- 如果函数中使用了默认参数的话,注意调用时的参数个数。
- 注意要读完
- 构造参数无法使用自己
- 树链剖分/dfs 序,初始化或者询问不要忘记 idx, ridx
- 排序时注意结构体的所有属性是不是考虑了
- 不要把 while 写成 if
- 不要把 int 开成 char
- 清零的时候全部用 0~n+1。
- 模意义下不要用除法
- 哈希不要自然溢出
- 最短路不要 SPFA,乖乖写 Dijkstra
- 上取整以及 GCD 小心负数
- mid 用
l + (r - l) / 2
可以避免溢出和负数的问题 - 小心模板自带的意料之外的隐式类型转换
- 求最优解时不要忘记更新当前最优解
- 图论问题一定要注意图不连通的问题
- 处理强制在线的时候 lastans 负数也要记得矫正
- 不要觉得编译器什么都能优化
- 分块一定要特判在同一块中的情况