Codeforces Round #753 (Div. 3) ABCDE

Posted 霜序0.2℃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #753 (Div. 3) ABCDE相关的知识,希望对你有一定的参考价值。


跳转到->Codeforces Round #753 (Div. 3)

A. Linear Keyboard

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a keyboard that consists of 2626 keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.

You have to type the word ss on this keyboard. It also consists only of lowercase Latin letters.

To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.

Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.

For example, consider a keyboard where the letters from ‘a’ to ‘z’ are arranged in consecutive alphabetical order. The letters ‘h’, ‘e’, ‘l’ and ‘o’ then are on the positions 88, 55, 1212 and 1515, respectively. Therefore, it will take |5−8|+|12−5|+|12−12|+|15−12|=13|5−8|+|12−5|+|12−12|+|15−12|=13 units of time to type the word “hello”.

Determine how long it will take to print the word ss.

Input

The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The next 2t2t lines contain descriptions of the test cases.

The first line of a description contains a keyboard — a string of length 2626, which consists only of lowercase Latin letters. Each of the letters from ‘a’ to ‘z’ appears exactly once on the keyboard.

The second line of the description contains the word ss. The word has a length from 11 to 5050 letters inclusive and consists of lowercase Latin letters.

Output

Print tt lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word ss on the given keyboard.

Example

input

Copy

5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba

output

Copy

13
0
68
0
74

代码与解释

无脑直接暴力即可

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\\n'
#define trav(a, x)  for(auto& a : x)
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define case        ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
#define _           0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
    (void)(cout << "L" << __LINE__ \\
    << ": " << #x << " = " << (x) << '\\n')
#define TIE \\
    cin.tie(0);cout.tie(0);\\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

template <typename T>
void read(T &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
    return;
}

inline void write(long long x) {
    if(x<0) putchar('-'), x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    putchar('\\n');
}

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI    = acos(-1.0);
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;
const int    maxn  = 1009;
const ll     N     = 5;

const double inf=1e20;
const double eps=1e-8;
const double pi=acos(-1.0);
const int maxp=1010;

string a,b;

int fun(char x) {
	for (int i=0; i<a.size(); i++) {
		if (x == a[i]) {
			return i;
		}
	}
}

void solve(){
	cin>>a>>b;
	int ans = 0;
	for (int i=1; i<b.size(); i++) {
		ans += abs(fun(b[i]) - fun(b[i-1]));
	}
	cout<<ans<<endl;
}	


int main()
{
//	TIE;

//	solve();
    case{solve();}
//    case{cout<<"Case "<<Q<<":"<<endl;solve();}
}

B. Odd Grasshopper

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The grasshopper is located on the numeric axis at the point with coordinate x0x0.

Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate xx with a distance dd to the left moves the grasshopper to a point with a coordinate x−dx−d, while jumping to the right moves him to a point with a coordinate x+dx+d.

The grasshopper is very fond of positive integers, so for each integer ii starting with 11 the following holds: exactly ii minutes after the start he makes a jump with a distance of exactly ii. So, in the first minutes he jumps by 11, then by 22, and so on.

The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.

For example, if after 1818 consecutive jumps he arrives at the point with a coordinate 77, he will jump by a distance of 1919 to the right, since 77 is an odd number, and will end up at a point 7+19=267+19=26. Since 2626 is an even number, the next jump the grasshopper will make to the left by a distance of 2020, and it will move him to the point 26−20=626−20=6.

Find exactly which point the grasshopper will be at after exactly nn jumps.

Input

The first line of input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

Each of the following tt lines contains two integers x0x0 (−1014≤x0≤1014−1014≤x0≤1014) and nn (0≤n≤10140≤n≤1014) — the coordinate of the grasshopper’s initial position and the number of jumps.

Output

Print exactly tt lines. On the ii-th line print one integer — the answer to the ii-th test case — the coordinate of the point the grasshopper will be at after making nn jumps from the point x0x0.

Example

input

Copy

9
0 1
0 2
10 10
10 99
177 13
10000000000 987654321
-433494437 87178291199
1 0
-1 1

output

Copy

-1
1
11
110
190
9012345679
-87611785637
1
0

Note

The first two test cases in the example correspond to the first two jumps from the point x0=0x0=0.

Since 00 is an even number, the first jump of length 11 is made to the left, and the grasshopper ends up at the point 0−1=−10−1=−1.

Then, since −1−1 is an odd number, a jump of length 22 is made to the right, bringing the grasshopper to the point with coordinate −1+2=1−1+2=1.

代码与解释

找规律,如果是偶数,假设从0开始,那么

0 -1 1 4 0 -5 1 8 …

可以观察规律,只要%4=0,那么就是0,%4=2,就是1

剩下的%1的是-1,-5,-9…,%3的是4,8,12…

这样规律就出来了

奇数也是类似如此

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\\n'
#define trav(a, x)  for(auto& a : x)
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define case        ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
#define _           0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
    (void)(cout << "L" << __LINE__ \\
    << ": " << #x << " = " << (x) << '\\n')
#define TIE \\
    cin.tie(0);cout.tie(0);\\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

template <typename T>
void read(T &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
    return;
}

inline void write(long long x) {
    if(x<0) putchar('-'), x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    putchar('\\n');
}

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI    = acos(-1.0);
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;
const int    maxn  = 1009;
const ll     N     = 5;

const double inf=1e20;
const double eps=1e-8;
const double pi=acos(-1.0);
const int maxp=1010;


void solve(){
	ll a,b;
	cin>>a>>b;
	if (a&1) {
		if (b % 4 == 0) {
			cout<<a<<endl;
		} else if (b % 4 == 2) {
			cout<<a-1<<endl;
		} else if (b % 4 == 1) {
			ll x = b/4;
			ll y = 4*x + 1;
			cout<<a+y<<endl;
		} else if (b % 4 == 3){
			ll x = b/4;
			ll y = (-4)*x -4;
			cout<<a + y<<endl;
		}
	} else {
		if (b % 4 == 0) {
			cout<<a<<endl;
		} else if (b % 4 == 2) {
			cout<<a+1<<endl;
		} else if (b % 4 == 1) {
			ll x = b/4;
			ll y = (-4)*x -1;
			cout<<a + y<<endl;
		} else以上是关于Codeforces Round #753 (Div. 3) ABCDE的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #753 (Div. 3) ABCD

Codeforces Round #753 (Div. 3) ABCD

Codeforces Round #753 (Div. 3) ABCD

Codeforces Round #753 (Div. 3) A-E

Codeforces Round #753 (Div. 3)(ABCDE)

Codeforces Round #436 E. Fire(背包dp+输出路径)