Codeforces Round #547 (Div. 3)

博客介绍了三道算法题。“Game 23”需将数 n 经乘 2 或乘 3 变换为 m,求最少步数;“Polycarp Restores Permutation”要根据差值数组还原排列;“Vova and Train”计算火车行驶中可见灯笼数量。还给出了各题的正确解法。

A. Game 23

Description

Polycarp plays "Game 23". Initially he has a number ?n and his goal is to transform it to ?m. In one move, he can multiply ?n by 22 or multiply ?n by 33. He can perform any number of moves.

Print the number of moves needed to transform ?n to ?m. Print -1 if it is impossible to do so.

It is easy to prove that any way to transform ?n to ?m contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).

Input

The only line of the input contains two integers ?n and ?m (1??51081≤n≤m≤5⋅108).

Output

Print the number of moves to transform ?n to ?m, or -1 if there is no solution.

Examples

Input

120 51840

Output

7

Note

In the first example, the possible sequence of moves is: 12024072014404320129602592051840.120→240→720→1440→4320→12960→25920→51840. The are 77 steps in total.

正确解法:

发现自己总有一种把难题想得特别复杂的那种,总之就是不敢去放手写。下一阶段要改正,不敢写也要写,哪怕错,哪怕超时,总能发现和正解错在哪里。

大半夜写CF神志不清。A题写不出来真是人才。

求m/n的 2的个数和3的个数,就是他可能无法变换乘几个2和几个3相乘。

 

C. Polycarp Restores Permutation

Description

An array of integers ?1,?2,,??p1,p2,…,pn is called a permutation if it contains each number from 11 to ?n exactly once. For example, the following arrays are permutations: [3,1,2][3,1,2], [1][1], [1,2,3,4,5][1,2,3,4,5] and [4,3,1,2][4,3,1,2]. The following arrays are not permutations: [2][2], [1,1][1,1], [2,3,4][2,3,4].

Polycarp invented a really cool permutation ?1,?2,,??p1,p2,…,pn of length ?n. It is very disappointing, but he forgot this permutation. He only remembers the array ?1,?2,,??1q1,q2,…,qn−1 of length ?1n−1, where ??=??+1??qi=pi+1−pi.

Given ?n and ?=?1,?2,,??1q=q1,q2,…,qn−1, help Polycarp restore the invented permutation.

Input

The first line contains the integer ?n (2?21052≤n≤2⋅105) — the length of the permutation to restore. The second line contains ?1n−1 integers ?1,?2,,??1q1,q2,…,qn−1 (?<??<?−n<qi<n).

Output

Print the integer -1 if there is no such permutation of length ?n which corresponds to the given array ?q. Otherwise, if it exists, print ?1,?2,,??p1,p2,…,pn. Print any such permutation if there are many of them.

Examples

Input

3
-2 1

Output

3 1 2 

正确解法:

设ans[1]=x,则ans[2]=x+p[1],ans[3]=x+p[1]+p[2],ans[4]=x+p[1]+p[2]+p[3];

则其中最小的就是1.因为是1-n的序列。

我们手上有p的序列,然后求前缀和。得到 { 0, p[1], p[1]+p[2],.....}

最小的与1求差值。 x+minn=1.  x=1-minn。

然后所有都加上x就是答案。

 1 include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 const int inf=0x7fffffff;
 9 const int N=200000+100;
10 int n,a[N],b[N],minn=0;
11 int main()
12 {
13     scanf("%d",&n);
14     for(int i=2;i<=n;i++)
15     {
16         scanf("%d",&a[i]);
17         a[i]=a[i-1]+a[i];
18         minn=min(minn,a[i]);
19     }
20     minn=-minn+1;
21     for(int i=1;i<=n;i++)
22     {
23         a[i]+=minn;
24         b[i]=a[i];
25     }
26     sort(b+1,b+n+1);
27     for(int i=1;i<=n;i++)
28         if(b[i]!=i)
29         {
30             cout<<-1<<endl;
31             return 0;
32         }
33     for(int i=1;i<=n;i++)
34         cout<<a[i]<<" ";
35     cout<<endl;
36 
37     return 0;
38 }
View Code

 

 

 

A. Vova and Train

Description

Vova plans to go to the conference by train. Initially, the train is at the point 11 and the destination point of the path is the point LL. The speed of the train is 11 length unit per minute (i.e. at the first minute the train is at the point 11, at the second minute — at the point 22 and so on).

There are lanterns on the path. They are placed at the points with coordinates divisible by vv (i.e. the first lantern is at the point vv, the second is at the point 2v2v and so on).

There is also exactly one standing train which occupies all the points from ll to rr inclusive.

Vova can see the lantern at the point pp if pp is divisible by vv and there is no standing train at this position (p[l;r]p∉[l;r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.

Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to tt different conferences, so you should answer tindependent queries.

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of queries.

Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1L,v1091≤L,v≤109, 1lrL1≤l≤r≤L) — destination point of the ii-th path, the period of the lantern appearance and the segment occupied by the standing train.

Output

Print tt lines. The ii-th line should contain one integer — the answer for the ii-th query.

Examples

Input

4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000

Output

3
0
1134
0

Note

A. Vova and Train

Description

Vova plans to go to the conference by train. Initially, the train is at the point 11 and the destination point of the path is the point LL. The speed of the train is 11 length unit per minute (i.e. at the first minute the train is at the point 11, at the second minute — at the point 22 and so on).

There are lanterns on the path. They are placed at the points with coordinates divisible by vv (i.e. the first lantern is at the point vv, the second is at the point 2v2v and so on).

There is also exactly one standing train which occupies all the points from ll to rr inclusive.

Vova can see the lantern at the point pp if pp is divisible by vv and there is no standing train at this position (p[l;r]p∉[l;r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.

Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to tt different conferences, so you should answer tindependent queries.

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of queries.

Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1L,v1091≤L,v≤109, 1lrL1≤l≤r≤L) — destination point of the ii-th path, the period of the lantern appearance and the segment occupied by the standing train.

Output

Print tt lines. The ii-th line should contain one integer — the answer for the ii-th query.

Examples

Input

4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000

Output

3
0
1134
0

Note

 
 

转载于:https://www.cnblogs.com/Kaike/p/10573035.html

标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值