ASC
import java.io.IOException;
class MC {
public void run(){
System.out.println((int)'L');
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
卡片
import java.io.IOException;
class MC {
int[] a = new int[10];
public void run(){
for (int i = 0; i < 10; i++) {
a[i] = 2021;
}
for (int i = 1; i < Integer.MAX_VALUE; i++) {
int x = i;
while (x > 0){
int t = x % 10;
if (a[t] > 0) a[t]--;
else{
System.out.println(i - 1);
return;
}
x /= 10;
}
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
直线
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
class Line{
double k, b;
public Line(double k, double b) {
this.k = k;
this.b = b;
}
}
class MC {
/**
* 确定一条直线靠斜率和截距,若斜率和截距有一个不同,则为不同的直线
* 枚举每两个点形成的斜率和截取,判断有多少个不同即可
* 还有特殊情况,斜率不存的情况单独拿出来(x1==x2),最后加上即可
* 20*20=400个点,假设每个都可形成不同直线,共400*400种情况
*/
int N = 200010;
Line[] l = new Line[N];
int m;
public void run(){
for (int x1 = 0; x1 < 20; x1++) {
for (int y1 = 0; y1 < 21; y1++) {
for (int x2 = 0; x2 < 20; x2++) {
for (int y2 = 0; y2 < 21; y2++) {
if(x1 == x2) continue;
double k = (double) (y2 - y1) / (x2 - x1);
double b = y1 - k * x1;
l[m++] = new Line(k, b);
}
}
}
}
Arrays.sort(l, 0, m, new Comparator<Line>() {
@Override
public int compare(Line o1, Line o2) {
if (o1.k == o2.k) {
if (o1.b < o2.b) return -1;
else return 1;
}
if (o1.k < o2.k){
return -1;
}else return 1;
}
});
int res = 1;
for (int i = 1; i < m; i++) {
if(Math.abs(l[i - 1].k - l[i].k) > 1e-8 || Math.abs(l[i - 1].b - l[i].b) > 1e-8) res++;
}
System.out.println(res + 20);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
货物摆放
范围太大不能死磕,看一下有什么特性来排除不可能的答案
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class MC {
//分解约数
List<Long> divide(long x){
List<Long> res = new ArrayList<>();
for (long i = 1; i <= x / i; i++) {
if(x % i == 0){
res.add(i);
if(x / i != i) res.add(x / i);
}
}
return res;
}
public void run(){
//先分解质因数去掉无效方案
long li = Long.parseLong("2021041820210418");
List<Long> list = divide(li);
int res = 0;
//枚举长
for (int i = 0; i < list.size(); i++) {
//枚举宽
for (int j = 0; j < list.size(); j++) {
//高也是一个整数,因此长*宽能整除总长度就是合法方案
if (li % (list.get(i) * list.get(j)) == 0) res++;
}
}
System.out.println(res);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
路径
最小公倍数+dijkstra
import java.io.IOException;
import java.util.Arrays;
import java.util.PriorityQueue;
class PII implements Comparable<PII>{
int first, second;
public PII(int first, int second) {
this.first = first;
this.second = second;
}
//按照距离升序排序
@Override
public int compareTo(PII o) {
return this.first - o.first;
}
}
class MC {
int N = (int) 1e6;
//无向图
int[] h = new int[N], ne = new int[N], e = new int[N], w = new int[N];
int idx;
int[] dist = new int[N];
boolean[] st = new boolean[N];
void add(int a, int b, int c){
e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx++;
}
int gcd(int a, int b){
return b > 0 ? gcd(b, a % b): a;
}
int lcm(int a, int b){
return a * b / gcd(a, b);
}
void dijkstra(){
PriorityQueue<PII> q = new PriorityQueue<>();
Arrays.fill(st, false);
Arrays.fill(dist, 0x3f3f3f3f);
dist[1] = 0;
q.add(new PII(0, 1));
while (q.size() > 0){
PII t = q.poll();
int dis = t.first, p = t.second;
if (st[p]) continue;
st[p] = true;
for (int i = h[p]; i != -1; i = ne[i]){
int j = e[i];
//w[i]:点i到点j的距离,dist[j]:点1到点j的距离
if(dis + w[i] < dist[j]) {
dist[j] = dis + w[i];
q.add(new PII(dist[j], j));
}
}
}
}
public void run(){
//初始化邻接表
Arrays.fill(h, -1);
idx = 0;
for (int i = 1; i <= 2021; i++) {
for (int j = 1; j <= 2021; j++) {
if (Math.abs(i - j) <= 21) {
add(i, j, lcm(i, j));
}
}
}
dijkstra();
System.out.println(dist[2021]);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
时间显示
import java.util.*;
class MC {
public void run(){
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
Calendar is = Calendar.getInstance();
is.setTimeInMillis(t);
System.out.printf("%02d:%02d:%02d", is.get(Calendar.HOUR_OF_DAY), is.get(Calendar.MINUTE), is.get(Calendar.SECOND));
}
}
public class Main {
public static void main(String[] args) {
new MC().run();
}
}
最少砝码
import java.util.Scanner;
class MC{
//砝码总重量不超过1e5,则m最大为1e5,因为要用到-m,所以空间要*2
//加上偏移后下标从0开始
int N = 110, M = (int) (2e5 + 10), base = (int) 1e5;
boolean[][] f = new boolean[N][M];
int[] w = new int[N];
public void run(){
int n, m = 0;
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
w[i] = sc.nextInt();
m += w[i];
}
//0个砝码称出0的重量
//f[0][0],base相当于0
f[0][base] = true;
//循环物品
for (int i = 1; i <= n; i++) {
//循环体积
for (int j = -m; j <= m; j++) {
//不选
f[i][j + base] = f[i - 1][j + base];
//放左边
if(j - w[i] >= -m) f[i][j + base] = f[i][j + base] | f[i - 1][j - w[i] + base];
//放右边
if(j + w[i] <= m) f[i][j + base] = f[i][j + base] | f[i - 1][j + w[i] + base];
}
}
//从1开始
int cnt = 0;
for (int j = 1; j <= m; j++) {
//前i个物品能否称出重量j
if (f[n][j + base]) cnt++;
}
System.out.println(cnt);
}
Scanner sc = new Scanner(System.in);
}
public class Main {
public static void main(String[] args) {
new MC().run();
}
}
杨辉三角形
N
∈
\in
∈[1,N]
第1个斜行C(N,0)
第2个斜行C(N,1)
第3个斜行C(N,2)
第n个斜行C(N,n)
C34 17大于1e9,C34 16小于1e9
我们只需枚举16斜行即可
若n在第k斜行的第l个,则对应的顺序位置为:(l + 1) * l/ 2 + l+ 1
import java.io.IOException;
import java.util.Scanner;
class MC {
int n;
long C(int a, int b){
long res = 1;
for (int i = 1, j = a; i <= b; i++, j --) {
res = res * j / i;
}
return res;
}
boolean check(int k){
int l = 2 * k, r = Math.max(n, l);
while (l < r){
//枚举N
int mid = l + r >> 1;
if (C(mid, k) >= n) r = mid;
else l = mid + 1;
}
if (C(l, k) != n) return false;
System.out.println(((long) l + 1) * l / 2 + k + 1);
return true;
}
public void run() {
//第1个斜行C(N,0)
//第2个斜行C(N,1)
//第3个斜行C(N,2)
//第n个斜行C(N,n)
//C34 17大于1e9,C34 16小于1e9
//我们只需枚举16斜行即可
//若n在第k斜行的第l个,则对应的顺序位置为:(l + 1) * l/ 2 + l+ 1
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 16; i >= 0; i--) {
if (check(i)) break;
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}