May 13, 2023

LitCTF2023

目录

小小小

Background

源码星球上有一种有趣的游戏「小,小小小」。

Description

给你 $n$ 张卡片,第 $i$ 张卡片上写着 $a_i$。

定义一个包含 $n$ 张卡片的卡片组分值为:

$$ \sum_{i=1}^{n}\lvert {a_i - a_{i + 1}}\rvert $$

其中 $a_{n+1} = a_1$。

卡片的顺序显然会影响其分值,所以你需要将其以某种方式排列以得到最小的分值。

因为游戏要求,你还需要对卡片的每个前缀计算将其排列后能得到的最小分值。

Format

Input

第一行一个正整数 $n$,表示卡片数。

第二行 $n$ 个整数,第 $i$ 个表示 $a_i$。

Output

$n$ 行每行一个整数,即你能从卡片 $[1, i]$ 中得到的最小分值。

Samples

in1

5
5 3 4 4 7

out1

0
4
4
4
8

Limitation

对于 $100 \%$ 的数据,$1 \le n \le 10^6, 0 \le a_i \lt 2^{31}$。


Prime

题目背景

打比赛怎么能没有质数呢?

题目描述

给你一个正整数 $p$,让你求最小的正整数 $n$ ,使得 $n!\equiv 0\pmod p$。

但由于 $p$ 很大,将给出 $m$ 和 $e_1\dots e_m$,表示 $p=\prod\limits_{i=1}^m pr_i^{e_i}$ ,其中 $pr_i$ 是从小到大第 $i$ 个质数。

Format

输入格式

第一行一个正整数 $m$。

第二行包含 $m$ 个非负整数,其中第 $i$ 个数字表示 $e_i$。

输出格式

输出共 $1$ 行,每行包含一个数字,表示该组数据的答案。

样例

input1

5
1 1 1 1 1

output1

11

input2

12
1 3 4 6 7 9 10 12 13 15 16 18

output2

666

提示

有一个绝妙的解释,但是这里太小,写不下。

$1\le m\le 100,0\le e_i\le10^{18}$

保证 $pr_i\times e_i\le10^{18}$。


SEEM和探姬的游戏

题目描述:

SEEM和探姬发明了一个新游戏。 游戏在树上进行,每个点有点权。 每次他们会选两个点 $s,t$,并将一个棋子放在点 $s$ 上。 他们会轮流移动棋子,到过的点不能再走,并且他们会保证最后移到 $t$ 上。 SEEM先手,分数一开始为 $a_s$。 若当前是SEEM移动的,分数就减去到达点的点权,否则就加上到达点的点权。 SEEM想要答案尽可能小,探姬会让答案尽可能地大。 设最后的分数是 $f_{s,t}$。 求 $\sum\limits_{i=1}^n\sum\limits_{j=1}^n f_{i,j}$。

Format

输入格式

第一行一个数 $n$,表示树的点数。 第二行 $n$ 个数,$a_i$ 表示第 $i$ 个点的点权。 接下来 $n-1$ 行,每行两个数 $x,y$ 表示 $x$ 到 $y$ 之间有一条边。

输出格式

一个数表示 $\sum\limits_{i=1}^n\sum\limits_{j=1}^n f_{i,j}$。 对 $10^9+7$ 取模。

样例

in1

4
-4 1 5 -2
1 2
1 3
1 4

out1

40

in2

8
-2 6 -4 -4 -9 -3 -7 23
8 2
2 3
1 4
6 5
7 6
4 7
5 8

out2

4

提示

数据保证 $2\le n \le 2\times 10^5,-10^9\le a_i \le10^9$。


口算题卡_PWN2

from pwn import *

HOST = 'node5.anna.nssctf.cn'
PORT = 28516


def solve_problem(problem):
    problem = problem[8:]
    problem = problem.replace("?", "")
    print(problem)
    # 根据题目字符串解析出操作数和操作符
    op1, operator, op2 = problem.split()
    op1, op2 = int(op1), int(op2)
    # 根据操作符计算出答案
    if operator == '+':
        result = op1 + op2
    elif operator == '-':
        result = op1 - op2
    else:
        raise ValueError('Invalid operator: {}'.format(operator))
    return result


def eat():
    conn.recvline()


# 连接到指定的IP地址和端口号
conn = remote(HOST, PORT, level='DEBUG')
for _ in range(20):
    eat()
for i in range(100):
    # 接收问题并输出
    # 循环十次

    problem_msg = conn.recvline()  # What is 92 - 15?
    print(problem_msg.decode().strip())
    # 解决问题并发送答案
    answer = solve_problem(problem_msg.decode())
    conn.sendline(str(answer).encode())
    # 接收回复消息并判断是否回答正确
    reply_msg = conn.recvline()
    if b'Wrong!' in reply_msg:
        print(reply_msg.decode().strip())
        break
    else:
        print('Correct!')
# 输出最终的回复消息
final_msg = conn.recvline()
print(final_msg.decode().strip())
# 关闭连接
conn.close()

欧拉?

其中 又因为的步骤为 欧拉定理

定理中 $gcd(a, n) = 1$ 的条件在此题中来自:

那道题n就两个素因子,都是512bit,m的比特数小于512,肯定互素啊 –KBU

import gmpy2
from Crypto.Util.number import long_to_bytes
 
n = 115140122725890943990475192890188343698762004010330526468754961357872096040956340092062274481843042907652320664917728267982409212988849109825729150839069369465433531269728824368749655421846730162477193420534803525810831025762500375845466064264837531992986534097821734242082950392892529951104643690838773406549
c = 406480424882876909664869928877322864482740577681292497936198951316587691545267772748204383995815523935005725558478033908575228532559165174398668885819826720515607326399097899572022020453298441
m=gmpy2.iroot(c,2)[0]
print(long_to_bytes(m))