凉肝的题解

╄→尐↘猪︶ㄣ 提交于 2020-01-16 02:07:44

A. Mezo Playing Zoma

Today, Mezo is playing a game. Zoma, a character in that game, is initially at position x=0. Mezo starts sending n commands to Zoma. There are two possible commands:

  • ‘L’ (Left) sets the position x:=x−1;
  • ‘R’ (Right) sets the position x:=x+1.

Unfortunately, Mezo’s controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position x doesn’t change and Mezo simply proceeds to the next command.
For example, if Mezo sends commands “LRLR”, then here are some possible outcomes (underlined commands are sent successfully):

  • “LRLR” — Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position 0;
  • “LRLR” — Zoma recieves no commands, doesn’t move at all and ends up at position 0 as well;
  • “LRLR” — Zoma moves to the left, then to the left again and ends up in position −2.

Mezo doesn’t know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at.

Input

The first line contains n (1≤n≤10510^5) — the number of commands Mezo sends.

The second line contains a string s of n commands, each either ‘L’ (Left) or ‘R’ (Right).

Output

Print one integer — the number of different positions Zoma may end up at.

Example

Input
4
LRLR
Output
5

Note

In the example, Zoma may end up anywhere between −2 and 2.

题解

相当于对字串里的字符进行任意的组合,取得的结果可能性范围是所有的RR加起来以及所有的LL加起来所形成的区间长度.

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;

int main()
{
    string s;
    int len = 0;
    cin >> len >> s;
    int cnt = 0, _cnt = 0;
    for (int i = 0; i < len; i++)
        if (s[i] == 'L')
            cnt++;
        else
            _cnt++;
    cout << (cnt + _cnt + 1) << endl;
    return 0;
}

B. Just Eat It!

Today, Yasser and Adel are at the shop buying cupcakes. There are n cupcake types, arranged from 1 to n on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type i is an integer ai. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.

Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.

On the other hand, Adel will choose some segment [l,r] (1lrn1≤l≤r≤n) that does not include all of cupcakes (he can’t choose [l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,,r.l,l+1,…,r.

After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice.

For example, let the tastinesses of the cupcakes be [7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=10. Adel can choose segments [7],[4],[−1],[7,4] or [4,−1], their total tastinesses are 7,4,−1,11 and 3, respectively. Adel can choose segment with tastiness 11, and as 10 is not strictly greater than 11, Yasser won’t be happy 😦

Find out if Yasser will be happy after visiting the shop.

Input

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

The first line of each test case contains n (2≤n≤105).

The second line of each test case contains n integers a1,a2,…,an (−109aia_i≤109), where ai represents the tastiness of the ithi_{th} type of cupcake.

It is guaranteed that the sum of n over all test cases doesn’t exceed 10510^5.

Output

For each test case, print “YES”, if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice. Otherwise, print “NO”.

Example

Input
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
Output
YES
NO
NO

Note

In the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.

In the second example, Adel will choose the segment [1,2] with total tastiness 11, which is not less than the total tastiness of all cupcakes, which is 10.

In the third example, Adel can choose the segment [3,3] with total tastiness of 5. Note that Yasser’s cupcakes’ total tastiness is also 5, so in that case, the total tastiness of Yasser’s cupcakes isn’t strictly greater than the total tastiness of Adel’s cupcakes.

题解

如果,这个序列存在一个非正的前缀和或者后缀和,我们删除它,这样,我们可以选出一个子串使得其和大于总和.
而对于其他的情况,我们可以知道,不存在能够让子串大于总和的情况了.

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;

int a[MAXN];
long long sum[MAXN], _sum[MAXN];
int main()
{

    int t;
    cin >> t;
    while (t--) {
        memset(sum, 0, sizeof(sum));
        memset(_sum, 0, sizeof(_sum));
        int n;
        cin >> n;
        bool f = 0;
        long long ans = 0;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            ans += a[i];
        }
        for (int i = 1; i <= n; i++) {
            sum[i] = sum[i - 1] + a[i];
            if (sum[i] <= 0)
                f = 1;
        }
        for (int i = n; i >= 1; i--) {
            _sum[i] = _sum[i + 1] + a[i];
            if (_sum[i] <= 0)
                f = 1;
        }
        if (f)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
    return 0;
}

C. Fadi and LCM

Today, Osama gave Fadi an integer XX, and Fadi was wondering about the minimum possible value of max(a,b)max(a,b) such that LCM(a,b)LCM(a,b) equals XX. Both aa and bb should be positive integers.

LCM(a,b)LCM(a,b) is the smallest positive integer that is divisible by both aa and bb. For example, LCM(6,8)=24,LCM(4,12)=12,LCM(2,3)=6LCM(6,8)=24, LCM(4,12)=12, LCM(2,3)=6.

Of course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair?

Input

The first and only line contains an integer X(1X1012X(1≤X≤10^{12}).

Output

Print two positive integers, aa and bb, such that the value of max(a,b)max(a,b) is minimum possible and LCM(a,b)LCM(a,b) equals XX. If there are several possible such pairs, you can print any.

Examples

Input
2
Output
1 2
Input
6
Output
2 3
Input
4
Output
1 4
Input
1
Output
1 1

题解

这个题目,就是给一个最小公倍数nn,求两个数a,ba,b.
还要求求出的两个数的最大值最小.
自然想到在给定数的因子里寻找aa.
再判断b=nab=\dfrac{n}{a},a,ba,b的最小公倍数是否为nn即可.

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;

typedef long long ll;
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
int main()
{
    ll n;
    cin >> n;
    ll a = sqrt(n);
    for (ll i = a; i >= 1; i--)
        if (n % i == 0) {
            ll b = n / i;
            ll g = gcd(i, b);
            if (i / g * b == n) {
                a = i;
                break;
            }
        }
    cout << a << ' ' << n / a << endl;
    return 0;
}

D. Dr. Evil Underscores

Today, as a friendship gift, Bakry gave Badawy nn integers a1,a2,,ana_1,a_2,…,a_n and challenged him to choose an integer XX such that the value max1in(aiX)\underset{1≤i≤n}{max}(a_i⊕X) is minimum possible, where denotes the bitwise XORXOR operation.

As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1in(aiX)\underset{1≤i≤n}{max}(a_i⊕X).

Input

The first line contains integer n(1n105)n (1≤n≤10^5).

The second line contains n integers a1,a2,,an(0ai2301)a_1,a_2,…,a_n (0≤a_i≤2^{30−1}).

Output

Print one integer — the minimum possible value of max1in(aiX)\underset{1≤i≤n}{max}(ai⊕X).

Examples

Input
3
1 2 3
Output
2
Input
2
1 5
Output
4

Note

In the first sample, we can choose X=3.

In the second sample, we can choose X=5.

题解

按位递归.

  • 如果该为有11也有00,我们将其分成两组,分别递归,因为两者都有的话,无论怎么XORXOR,所得结果在该位都有11,于是ans=ans+2kans=ans+2^{k}
  • 如果只有11或者只有00,直接递归,因为可以避免该位XORXOR11

依次递归,求得结果

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;

int dfs(vector<int> v, int bit)
{
    if (bit < 0)
        return 0;
    vector<int> zero, one;
    for (auto it = v.begin(); it != v.end(); it++)
        if (1 << bit & (*it))
            one.push_back(*it);
        else
            zero.push_back(*it);
    if (one.size() == 0)
        return dfs(zero, bit - 1);
    else if (zero.size() == 0)
        return dfs(one, bit - 1);
    else
        return min(dfs(one, bit - 1), dfs(zero, bit - 1)) + (1 << bit);
}
int main()
{
    int n;
    cin >> n;
    vector<int> v;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        v.push_back(x);
    }
    cout << dfs(v, 30);
    return 0;
}

E. Deadline

Adilbek was assigned to a special project. For Adilbek it means that he has n days to run a special program and provide its results. But there is a problem: the program needs to run for d days to calculate the results.

Fortunately, Adilbek can optimize the program. If he spends xx (xx is a non-negative integer) days optimizing the program, he will make the program run in dx+1⌈\frac{d}{x+1}⌉ days (a⌈a⌉ is the ceiling function: 2.4=3,2=2⌈2.4⌉=3, ⌈2⌉=2). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to x+dx+1x+⌈\frac{d}{x+1}⌉.

Will Adilbek be able to provide the generated results in no more than nn days?

Input

The first line contains a single integer T(1T50)T (1≤T≤50) — the number of test cases.

The next TT lines contain test cases – one per line. Each line contains two integers nn and d(1n109,1d109)d (1≤n≤10^9, 1≤d≤10^9) — the number of days before the deadline and the number of days the program runs.

Output

Print T answers — one per test case. For each test case print YES (case insensitive) if Adilbek can fit in n days or NO (case insensitive) otherwise.

Example

Input
3
1 1
4 5
5 11
Output
YES
YES
NO

Note

In the first test case, Adilbek decides not to optimize the program at all, since dnd≤n.

In the second test case, Adilbek can spend 1 day optimizing the program and it will run 52⌈\frac{5}{2}⌉=3 days. In total, he will spend 4 days and will fit in the limit.

In the third test case, it’s impossible to fit in the limit. For example, if Adilbek will optimize the program 2 days, it’ll still work 112+1⌈\frac{11}{2+1}⌉=4 days.

题解

给一个天数nn,以及工作需要时间dd,以及一个优化时间的方式,将dd转变为x+dx+1x+⌈\frac{d}{x+1}⌉.
也就是判断dd是否小于nn,最小的x+dx+1x+⌈\frac{d}{x+1}⌉能否小于nn.
对于x+dx+1x+\frac{d}{x+1},我们转换为x+1+dx+11x+1+\frac{d}{x+1}-1
我们根据均值不等式可知,当x+1=dx+1x+1=\frac{d}{x+1}时,可以得到上式得最小值
x=d1x=\sqrt{d}-1

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;

typedef long long ll;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n, d;
        cin >> n >> d;
        int x = sqrt(d);
        int minn = d;
        for (int i = max(x - 2,1); i <= x + 1; i++) {
            int temp = i + d / (i + 1) + (d % (i + 1) != 0);
            minn = min(temp, minn);
        }
        if (minn <= n)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

F. Yet Another Meme Problem

Try guessing the statement from this picture http://tiny.cc/ogyoiz.
You are given two integers AA and BB, calculate the number of pairs (a,b)(a,b) such that 1aA,1bB1≤a≤A, 1≤b≤B, and the equation ab+a+b=conc(a,b)a⋅b+a+b=conc(a,b) is true; conc(a,b)conc(a,b) is the concatenation of aa and bb (for example, conc(12,23)=1223,conc(100,11)=10011conc(12,23)=1223, conc(100,11)=10011). aa and bb should not contain leading zeroes.

Input

The first line contains t(1t100)t (1≤t≤100) — the number of test cases.
Each test case contains two integers AA and B(1A,B109)B (1≤A,B≤10^9).

Output

Print one integer — the number of pairs (a,b)(a,b) such that 1aA,1bB1≤a≤A, 1≤b≤B, and the equation ab+a+b=conc(a,b)a⋅b+a+b=conc(a,b) is true.

Example

Input
3
1 11
4 2
191 31415926
Output
1
0
1337

题解

我们考虑这个conc(a,b)conc(a,b),其值就相当于a10n+ba\cdot 10^n+b
那么对于ab+a+b=a10n+ba\cdot b+a+b=a\cdot 10^n+b
化简就得到了a(b+1)=a10na(b+1)=a\cdot 10^n
我们可以得到b=10n1b=10^n-1
aa得取值便为AA得范围,bb得取值为9,99,999...9,99,999...

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;

typedef long long ll;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        long long A, B;
        cin >> A >> B;
        long long cnt = 0, ans = 0;
        while (cnt <= B) {
            cnt = cnt*10+9;
            ans++;
        }
        ans--;
        cout << A * ans << endl;
    }
    return 0;
}

G. HQ9+

HQ9+ is a joke programming language which has only four one-character instructions:

  • “H” prints “Hello, World!”,
  • “Q” prints the source code of the program itself,
  • “9” prints the lyrics of “99 Bottles of Beer” song,
  • “+” increments the value stored in the internal accumulator.

Instructions “H” and “Q” are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored.
You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output.

Input

The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive.

Output

Output “YES”, if executing the program will produce any output, and “NO” otherwise.

Examples

Input
Hi!
Output
YES
Input
Codeforces
Output
NO

Note

In the first case the program contains only one instruction — “H”, which prints “Hello, World!”.
In the second case none of the program characters are language instructions.

题解

如题所述,包含’H’,‘Q’,和9得时候就输出YES

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;
typedef long long ll;
char s[110];
int main()
{
	cin >> s;
	int len = strlen(s);
	for(int i=0;i<len;i++)
		if (s[i] == 'H' || s[i] == 'Q' || s[i] == '9') {
			cout << "YES" << endl;
			return 0;
		}
	cout << "NO" << endl;
	return 0;
}

H. Rolling The Polygon

Bahiyyah has a convex polygon with nn vertices P0,P1,,Pn1P_0,P_1,⋯,P_{n−1} in the counterclockwise order. Two vertices with consecutive indexes are adjacent, and besides, P0P_0 and Pn1P_{n−1} are adjacent. She also assigns a point QQ inside the polygon which may appear on the border.

Now, Bahiyyah decides to roll the polygon along a straight line and calculate the length of the trajectory (or track) of point QQ.

To help clarify, we suppose Pn=P0,Pn+1=P1P_n=P_0,P_{n+1}=P_1 and assume the edge between P0P_0 and P1P_1 is lying on the line at first. At that point when the edge between Pi1P_{i−1} and PiP_i lies on the line, Bahiyyah rolls the polygon forward rotating the polygon along the vertex PiP_i until the next edge (which is between PiP_i and Pi+1P_{i+1}) meets the line. She will stop the rolling when the edge between PnP_n and Pn+1P_{n+1} (which is same as the edge between P0P_0 and P1P_1) meets the line again.

Input

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 50.

For each test case, the first line contains an integer n(3n50)n (3≤n≤50) indicating the number of vertices of the given convex polygon. Following n lines describe vertices of the polygon in the counterclockwise order. The i-th line of them contains two integers xi1x_{i−1} and yi1y_{i−1}, which are the coordinates of point Pi1P_{i−1}. The last line contains two integers xQx_Q and yQy_Q, which are the coordinates of point QQ.

We guarantee that all coordinates are in the range of 103−10^3 to 10310^3, and point QQ is located inside the polygon or lies on its border.

Output

For each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y is the length of the trajectory of the point Q rounded to 3 places. We guarantee that 4-th place after the decimal point in the precise answer would not be 4 or 5.

Example

Input
4
4
0 0
2 0
2 2
0 2
1 1
3
0 0
2 1
1 2
1 1
5
0 0
1 0
2 2
1 3
-1 2
0 0
6
0 0
3 0
4 1
2 2
1 2
-1 1
1 0
Output
Case #1: 8.886
Case #2: 7.318
Case #3: 12.102
Case #4: 14.537

题解

这个题的意思就是给一个凸包,凸包里面有个点,我们滚动这个凸包,问这个点它走过的路径是多少
1
拿这个图说吧,就是ABAB转到了NBNB的位置,BCBC转到了BIBI的位置
我们可以知道ABN=CBI=HBH\angle{ABN}=\angle{CBI}=\angle{HBH'},毕竟旋转的角度是一样的啊
然后咧,我们求HH\overset{\frown}{HH'}的长度,就相当于求圆弧嘛.
BB为圆点,HBH\angle{HBH'}的角所对应嘛
然后求HBH\angle{HBH'}相当于求ABN\angle{ABN}相当于求πNBI\pi-\angle{NBI}
然后就结束了

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const double pi = acos(-1.0);
const int inf = __LONG_MAX__;
const int enf = LONG_MIN;
const long long INF = LLONG_MAX;
const long long ENF = LLONG_MIN;
const double eps = 1e-12;

typedef struct point vec;
struct point { //点的基本数据结构
    double x, y;
    double poe;
    point(double _x = 0, double _y = 0)
        : x(_x)
        , y(_y)
    {
    }
    double len() //模长
    {
        return sqrt(x * x + y * y);
    }
    vec chuizhi()
    {
        return vec(-y, x);
    }
    double operator*(const point& i_T) const //点积
    {
        return x * i_T.x + y * i_T.y;
    }
    double operator^(const point& i_T) const //叉积
    {
        return x * i_T.y - y * i_T.x;
    }
    point operator*(double u) const
    {
        return point(x * u, y * u);
    }
    bool operator==(const point& i_T) const
    {
        return fabs(x - i_T.x) < eps && fabs(y - i_T.y) < eps;
    }
    point operator/(double u) const
    {
        return point(x / u, y / u);
    }
    point operator+(const point& i_T)
    {
        return point(x + i_T.x, y + i_T.y);
    }
    point operator-(const point& i_T)
    {
        return point(x - i_T.x, y - i_T.y);
    }
    friend bool operator<(point a, point b)
    {
        return fabs(a.y - b.y) < eps ? a.x < b.x : a.y < b.y;
    }
    void atn2()
    {
        poe = atan2(y, x);
    }
    friend ostream& operator<<(ostream& out, point& a)
    {
        //cout << a.x << ' ' << a.y;
        printf("%.8f %.8f", a.x, a.y);
        return out;
    }
    friend istream& operator>>(istream& in, point& a)
    {
        scanf("%lf%lf", &a.x, &a.y);
        return in;
    }
} p[100];

double jiajiao(vec a, vec b)
{
    return pi - acos(a * b / (a.len() * b.len()));
}
double d[100];
int main()
{
    point p0;
    int n, t, cas = 1;
    scanf("%d", &t);
    for (int k = 1; k <= t; k++) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            cin >> p[i];
        cin >> p0;
        for (int i = 1; i <= n; i++)
            d[i] = (p0 - p[i]).len();
        p[0] = p[n], p[n + 1] = p[1];
        double ans = 0;
        for (int i = 1; i <= n; i++)
            ans += d[i] * jiajiao(p[i - 1] - p[i], p[i + 1] - p[i]);
        printf("Case #%d: %.3f\n", k, ans);
    }
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!