1.8总结

A - A

Problem Statement

You are given an integer k. Find the largest integer x, where1≤x<k, such that x! + (x - 1)! is a multiple of k, or determine that no such x exists.

y! denotes the factorial of yy, which is defined recursively as y!=y⋅(y−1)! for y≥1 with the base case of0!=1. For example, 5!=5⋅4⋅3⋅2⋅1⋅0!=120.

If a and b are integers, then a is a multiple of b if there exists an integer c such that a=bc. For example, 10is a multiple of 5 but 9 is not a multiple of 6.

Input

The first line contains a single integer t(1≤t≤10^4) — the number of test cases. The description of test cases follows.

The only line of each test case contains a single integer k(2≤k≤10^9).

Output

For each test case output a single integer — the largest possible integer x that satisfies the conditions above.

If no such xx exists, output -1.

Sample 1

Inputcopy

Outputcopy

4

3

6

8

10

2

5

7

9

Note

In the first test case, 2!+1!=2+1=3, which is a multiple of 3.

In the third test case, 7!+6!=5040+720=5760, which is a multiple of 8.

还好眼尖的我发现了规律。题目大意是找一个最大的数x,使得x!+(x-1)!是k的倍数。通过分析我们可以知道

k=x-1
k!=k*(k-1)!
k!+(k-1)!=(k+1)*(k-1)!=x*(k-1)!
所以最大的数就是k

B - B

Problem Statement

Takahashi is a cashier.

There is a cash register with 11keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The cash register initially displays 00. Whenever he types the key 00, the displayed number is multiplied by 100; whenever he types one of the others, the displayed number is multiplied by 10, and then added by the number written on the key.

Takahashi wants the cash register to display an integer S. At least how many keystrokes are required to make it display S?

Constraints

  • 1≤S≤10100000

  • S is an integer.

Input

The input is given from Standard Input in the following format:

S

Output

Print the answer in a line.

Sample 1

Inputcopy

Outputcopy

40004

4

For example, the following four keystrokes make the cash register display 4000440004. Initially, the cash register displays 0.

  • Type the key 4. It now displays 4.

  • Type the key 00. It now displays 400.

  • Type the key 0. It now displays 4000.

  • Type the key 4. It now displays 40004.

He cannot make it display 40004 with three or fewer keystrokes, so 44 should be printed.

Sample 2

Inputcopy

Outputcopy

1355506027

10

Sample 3

Inputcopy

Outputcopy

10888869450418352160768000001

27

Note that S may not fit into a 64 operatorname bit integer type.

通过样例不难发现,除了有0连续出现时有不同,其他的都一样。所以只需要统计连续出现两个0的次数,再拿总长减去就是答案了。要注意数组应该开大一点,不然就会运行错误的。

D - D

Problem Statement

You are given a simple undirected graph with N vertices numbered 1 to N and MM edges numbered 1 to M. Edge i connects vertex ui and vertex vi.

Find the number of connected components in this graph.

Notes

A simple undirected graph is a graph that is simple and has undirected edges.

A graph is simple if and only if it has no self-loop or multi-edge.

A subgraph of a graph is a graph formed from some of the vertices and edges of that graph.

A graph is connected if and only if one can travel between every pair of vertices via edges.

A connected component is a connected subgraph that is not part of any larger connected subgraph.

Constraints

  • 1≤N≤100

  • 0≤M≤N(N−1)/2

  • 1≤ui,vi≤N

  • The given graph is simple.

  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M

u1v1

u2v2

……

uM​ vM

Output

Print the answer.

Sample 1

Inputcopy

Outputcopy

5 3

1 2

1 3

4 5

2

The given graph contains the following two connected components:

  • a subgraph formed from vertices 1, 2, 3, and edges 1, 2;

  • a subgraph formed from vertices 4, 5, and edge 3.

Sample 2

Inputcopy

Outputcopy

5 0

5

Sample 3

Inputcopy

Outputcopy

4 6

1 2

1 3

1 4

2 3

2 4

3 4

1

看了好久的题目,大意就是让我们用并查集合并之后数有多少个根结点就可以了。

E - E

MKnez wants to construct an arrays1,s2,…,sn satisfying the following conditions:

  • Each element is an integer number different from 0;

  • For each pair of adjacent elements their sum is equal to the sum of the whole array.

More formally,si!=0 must hold for each 1≤in. Moreover, it must hold that }s1+s2+⋯+sn=si+si+1 for each1≤i<n.

Help MKnez to construct an array with these properties or determine that it does not exist.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). The description of the test cases follows.

The only line of each test case contains a single integer n (2≤n≤1000) — the length of the array.

Output

For each test case, print "YES" if an array of length nn satisfying the conditions exists. Otherwise, print "NO". If the answer is "YES", on the next line print a sequence s1,s2,…,sn satisfying the conditions. Each element should be a non-zero integer in the range [-5000,5000][−5000,5000], i. e−5000≤si≤5000 and si!=0 should hold for each 1≤in.

It can be proved that if a solution exists then there also exists one which satisfies the additional constraints on the range.

If there are several correct answers, print any of them.

Sample 1

Inputcopy

Outputcopy

2

2

3

YES

9 5

NO

Note

In the first test case, [9,5] is a valid answer since 9+5 (the sum of the two adjacent elements s1+s2) is equal to 9+5 (the sum of all elements). Other solutions include [6,−9],[−1,−2],[−5000,5000],…

For the second test case, let us show why some arrays do not satisfy the constraints:

  • [1,1,1] —s1+s2=1+1=2 ands1+s2+s3=1+1+1=3 differ;

  • [1,-1,1]— s1+s2=1+(−1)=0 and s1+s2+s3=1+(−1)+1=1 differ;

  • [0,0,0] — The array s cannot contain a 0.

This is not a proof, but it can be shown that the answer is "NO".

想秃噜头了也没想到奇数n也有这样的序列。只不过3是个特例,就它没有。例如5就可以是-1,2,-1,2,-1

所以可以建立这样的关系式,令序列中的两个数为x1,x2

t=n/2
x1+x2=1
t+x1=1
>>x1=-t+1 x2=t

F - F

Problem Statement

We have a playlist with NNsongs numbered 1,…,N.

Song i lasts Ai seconds.

When the playlist is played, song 1, song 2, , and song N play in this order. When song N ends, the playlist repeats itself, starting from song 1 again. While a song is playing, the next song does not play; when a song ends, the next song starts immediately.

At exactly T seconds after the playlist starts playing, which song is playing? Also, how many seconds have passed since the start of that song?

There is no input where the playlist changes songs at exactly T seconds after it starts playing.

Constraints

  • 1≤N≤10^5

  • 1≤T≤10^18

  • 1≤Ai≤10^9

  • The playlist does not change songs at exactly T seconds after it starts playing.

  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N T

A1……AN

Output

Print an integer representing the song that is playing at exactly T seconds after the playlist starts playing, and an integer representing the number of seconds that have passed since the start of that song, separated by a space.

Sample 1

Inputcopy

Outputcopy

3 600

180 240 120

1 60

When the playlist is played, the following happens. (Assume that it starts playing at time 00.)

  • From time 0 to time 180, song 1plays.

  • From time 180 to time 420, song 2 plays.

  • From time 420 to time 540, song 3 plays.

  • From time 540 to time 720, song 1 plays.

  • From time 720 to time 960, song 2 plays.

  • dots⋮

At time 600, song 1 is playing, and60 seconds have passed since the start of that song.

Sample 2

Inputcopy

Outputcopy

3 281

94 94 94

3 93

Sample 3

Inputcopy

Outputcopy

10 5678912340

1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

6 678912340

小水题,只不过要把T提前模一下,不然就会时间超限。

还有两道比较难的题目暂时没搞出来

P1111 修复公路

题目背景

A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车。政府派人修复这些公路。

题目描述

给出A地区的村庄数N,和公路数M,公路是双向的。并告诉你每条公路的连着哪两个村庄,并告诉你什么时候能修完这条公路。问最早什么时候任意两个村庄能够通车,即最早什么时候任意两条村庄都存在至少一条修复完成的道路(可以由多条公路连成一条道路)

输入格式

第11行两个正整数N,M

下面M行,每行3个正整数x,y,t,告诉你这条公路连着x,y两个村庄,在时间t时能修复完成这条公路。

输出格式

如果全部公路修复完毕仍然存在两个村庄无法通车,则输出−1,否则输出最早什么时候任意两个村庄能够通车。

输入输出样例

输入 #1

4 4

1 2 6

1 3 4

1 4 5

4 2 3

输出 #1

5

说明/提示

N≤1000,M≤100000

xN,yN,t≤100000

困扰了好几天的题目,原来是用的冒泡排序所以时间超限了,这个只需要用并查集,计算那些已经相通了,因为不需要每两个村子之间都有路,所以计算到N=1是就已经完全相通了。

P2078 朋友

题目背景

小明在 A 公司工作,小红在 B 公司工作。

题目描述

这两个公司的员工有一个特点:一个公司的员工都是同性。

A 公司有 N 名员工,其中有 P对朋友关系。B 公司有 M名员工,其中有 Q对朋友关系。朋友的朋友一定还是朋友。

每对朋友关系用两个整数(Xi,Yi) 组成,表示朋友的编号分别为 Xi,Yi。男人的编号是正数,女人的编号是负数。小明的编号是 1,小红的编号是 −1。

大家都知道,小明和小红是朋友,那么,请你写一个程序求出两公司之间,通过小明和小红认识的人最多一共能配成多少对情侣(包括他们自己)。

输入格式

输入的第一行,包含 4 个空格隔开的正整数N,M,P,Q

之后 P 行,每行两个正整数 Xi,Yi

之后 Q 行,每行两个负整数Xi,Yi

输出格式

输出一行一个正整数,表示通过小明和小红认识的人最多一共能配成多少对情侣(包括他们自己)。

输入输出样例

输入 #1

4 3 4 2

1 1

1 2

2 3

1 3

-1 -2

-3 -3

输出 #1

2

说明/提示

对于30% 的数据,N,M≤100,P,Q≤200;

对于80% 的数据,N,M≤4×10^3,P,Q≤10^4;

对于100% 的数据,N,M≤10^4,P,Q≤2×10^4。

开始想要设两个数组,一个存A,一个存B,但是不知道为什么会运行错误。所以改成了用一个数组存,B公司的就存成N+1之后id数组内。只不过小明和小红也算在里面,所以统计人数要从1开始。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值