SDUT 2022 Summer Individual Contest - 11(for 21)

合集:

C - Large GCD

This problem is very simple so the problem setters decided that its statement should be simple too. You are given two integers nn and mm such that \text{gcd}(n,\,m) \equiv 1gcd(n,m)≡1, and your task is to find the value of the function \text{F}(n,\,m)F(n,m) as follows:

\text{F}(n,\,m) = \text{gcd}(5^n + 7^n,\,5^m + 7^m)F(n,m)=gcd(5n+7n,5m+7m)

In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 88 and 1212 is 44.

Input

The first line contains an integer TT (1 \le T \le 10^51≤T≤105) specifying the number of test cases.

Each test case consists of a single line contains two integers nn and mm (1 \le n, m \le 10^9,\, \text{gcd}(n,\,m) \equiv 11≤n,m≤109,gcd(n,m)≡1).

Output

For each test case, print a single line containing the value of the function \text{F}(n,\,m)F(n,m) as described in the statement.

Sample 1

InputcopyOutputcopy
2
2 3
5 3
2
12

Note

In the first test case, the value of the function can be found as follow:\text{F}(2,\,3) = \text{gcd}(5^2 + 7^2,\,5^3 + 7^3)F(2,3)=gcd(52+72,53+73)\text{F}(2,\,3) = \text{gcd}(74,\, 468)F(2,3)=gcd(74,468)\text{F}(2,\,3) = 2F(2,3)=2

In the second test case, the value of the function can be found as follow:\text{F}(5,\,3) = \text{gcd}(5^5 + 7^5,\,5^3 + 7^3)F(5,3)=gcd(55+75,53+73)\text{F}(5,\,3) = \text{gcd}(19932,\, 468)F(5,3)=gcd(19932,468)\text{F}(5,\,3) = 12F(5,3)=12

数论题,打表发现规律,证明原理啥的不太懂。。

/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 16*/
#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
int gcd(int a,int b){
	return b ? gcd(b,a%b) : a;
}
//
signed main(){
	IOS;
	int t;
	cin>>t;
	ll n,m;
	while(t--){
		cin>>n>>m;
		if(n%2==0||m%2==0) cout<<"2"<<endl;
		else cout<<"12"<<endl;
	}
}
//made by shun 20220719

E - Building Strings

You are given a string ss of length nn consisting of lowercase English letters. This string can be used to build other strings. The cost of each letter in ss is given by another string cc of length nn consisting of digits, such that the cost of using the letter s_isi​ is c_ici​ coins.

Also, you are given another string pp of length mm consisting of unique lowercase English letters. Your task is to find the minimum cost to build string pp by using the letters of ss. Can you?

Input

The first line contains an integer TT (1 \le T \le 5001≤T≤500) specifying the number of test cases.

The first line of each test case contains two integers nn and mm (1 \le n \le 10^3,\,1 \le m \le 261≤n≤103,1≤m≤26), in which nn is the length of strings ss and cc, and mm is the length of string pp.

Then 33 lines follow, each line contains a string, giving the string ss, cc, and pp, respectively. Both strings ss and pp contains only lowercase English letters, while string cc contains only digits. Also, string pp is consisting of unique letters.

Output

For each test case, print a single line containing the minimum cost of building the string pp by using the letters of string ss. If string pp cannot be built using string ss, print -1−1.

Sample 1

InputcopyOutputcopy
3
4 2
abcd
1234
ac
4 4
abcd
1234
abec
5 3
abcba
24513
acb
4
-1
8

Note

In the first test case, you have to use the 1^{st}1st and 3^{rd}3rd letters of string ss to build string pp. So, the total cost is 1 + 3 = 41+3=4.

In the second test case, you cannot build string pp using ss because the letter 'e' from pp does not exist in ss. So, the answer is -1−1.

In the third test case, the optimal way is to use the 1^{st}1st, 3^{rd}3rd, and 4^{th}4th letters of string ss to build pp. So, the total cost is 2 + 5 + 1 = 82+5+1=8.

Sponsor

水题,注意一点,一个数可能出现多次但是值可能不同

/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 16*/
#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
signed main(){
	IOS;
	int t;
	cin>>t;
	int n,m;
	string s,p,c;
	while(t--){
		bool judge=true;
		int res=0;
		cin>>n>>m;
		cin>>s>>c>>p;
		map<char,int> mp;
		//mem(mp,0x3f);
		for(int i=0;i<26;i++) mp[i+'a']=-1;
		for(int i=0;i<n;i++){
			if(mp[s[i]]==-1||mp[s[i]]>c[i]-'0') mp[s[i]]=c[i]-'0';
		}
		for(int i=0;i<m;i++){
			if(mp[p[i]]==-1){
				judge=false;
				break;
			}
			res+=mp[p[i]];
		}
		if(!judge) cout<<"-1"<<endl;
		else cout<<res<<endl;
	}
}
//made by shun 20220719

F - camelCase;

Camel case is a naming convention practice for multi-word identifiers in programming languages. In this practice, each word except for the first word will begin with an uppercase letter, with no intervening spaces or punctuation.

For example, "problem", "contestName", and "contestStartingTime" are all following the camel case practice, while "Server", "ProblemName", and "first-Place" are not.

in this problem, you are given a variable name that is following the camel case naming practice, and your task is to determine if the variable name is accepted or not. A variable name is accepted if it is consisting of no more than 77 words.

Input

The first line contains an integer TT (1 \le T \le 1001≤T≤100) specifying the number of test cases.

Each test case consists of a single line containing a string ss of length no more than 100100, giving a variable name. It is guaranteed that the variable name is following the camel case practice as described in the statement, and it contains only lowercase and uppercase English letters.

Output

For each test case, print a single line containing "YES" (without quotes) if the given variable name is accepted. Otherwise, print "NO" (without quotes).

Sample 1

InputcopyOutputcopy
2
weFoundYou
isTheCamelCaseTheBestWayToNameAVariable
YES
NO

Note

In the first test case, the variable name consists of 33 words, which are: "we", "Found", and "You". So, this variable name is accepted.

In the second test case, the variable name consists of 1111 words, which are: "is", "The", "Camel", "Case", "The", "Best", "Way", "To", "Name", "A", "Variable". So, this variable name is not accepted.

水题,注意几点,一是开头不能是大写字母,二是前面一个单词不要忘记加上,也就是sum的初值应该是1(因为这个细节wa了一发)

/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 16*/
#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
signed main(){
	IOS;
	string s;
	int t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		if(isupper(s[0])){
			cout<<"NO"<<endl;
			continue;
		}
		int num=1;
		for(int i=0;i<s.length();i++){
			if(isupper(s[i]))
				num++;
		}
		if(num>7) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
}
//made by shun 20220719

G - The Special King

Assem bought a new chess board that has a special king that move in a different way than other regular kings.

In one move, the special king can move from its position in one of the following directions: up, down, left, or right. Formally, if the special king is standing on position (xx, yy), in one move it can go to one of the following positions: (x - 1x−1, yy), (x + 1x+1, yy), (xx, y - 1y−1), or (xx, y + 1y+1).

Initially, the special king is standing on position (x_1x1​, y_1y1​) and Assem wants to place it on position (x_2x2​, y_2y2​). Can you help Assem by calculating the minimum number of required moves he needs to accomplish his goal?

Input

The first line contains an integer TT (1 \le T \le 40961≤T≤4096) specifying the number of test cases,

Each test consists of a single line containing four integers x_1x1​, y_1y1​, x_2x2​, and y_2y2​ (1 \le x_1, y_1, x_2, y_2 \le 81≤x1​,y1​,x2​,y2​≤8), in which x_1x1​ and y_1y1​ are representing the starting position of the special king, and x_2x2​ and y_2y2​ are representing the the ending position.

Output

For each test case, print a single line containing the minimum number of required moves to move the special king from the starting position to the ending position.

Sample 1

InputcopyOutputcopy
3
1 3 4 2
5 7 3 1
3 2 3 2
4
8
0

Note

In the first test case, the special king needs to be moved from position (11, 33) to (44, 22). One possible solution is to make 33 moves down to position (44, 33), then make 11 move to the left to position (44, 22). So, the total number of moves is 44.

水题,说是求最短,然而emm,没什么花样

/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 16*/
#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
signed main(){
	IOS;
	string s;
	int t;
	cin>>t;
	while(t--){
		int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		cout<<abs(x1-x2)+abs(y1-y2)<<endl;
	}
}
//made by shun 20220719

H - The Universal String

Have you ever heard about the universal string? This is the largest string in the world, and it is the mother of all strings that will ever exist in any programming language!

The universal string is an infinite string built by repeating the string "abcdefghijklmnopqrstuvwxyz" infinitely. So, the universal string will look like this:

"....nopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm...."

You are given a string pp and your task is to determine if pp is a substring of the universal string. Can you?

A substring of ss is a non-empty string xx = ss[ll\dots… rr] = s_lsl​s_{l+1}sl+1​\dots… s_rsr​ (1 \le≤ ll \le≤ rr \le≤ |ss|). For example, "code" and "force" are substring of "codeforces", while "coders" is not.

Input

The first line contains an integer TT (1 \le T \le 10^31≤T≤103) specifying the number of test cases.

Each test consists of a single line containing a non-empty string pp of length no more 10^3103 and consisting only of lowercase English letters.

Output

For each test case, print "YES" (without quotes) if the given string is a substring of the universal string. Otherwise, print "NO" (without quotes).

Sample 1

InputcopyOutputcopy
3
abcde
abd
wxyzabc
YES
NO
YES

因为是abcde。。。循环的一个串,所以要注意一点就是。。xyzabc。。的时候,这里判断一下,然后就是符合字母序就可以

/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 16*/
#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
signed main(){
	IOS;
	string s;
	int t;
	cin>>t;
	while(t--){
		cin>>s;
		int a[1005];
		bool res=false;
		for(int i=0;i<s.size();i++){
			a[i]=s[i]-'a';
		}
		for(int i=1;i<s.size();i++){
			if((a[i]==0&&a[i-1]!=25)||(a[i]!=0&&a[i]!=a[i-1]+1)){
				res=true;
				break;
			}
		}
		if(res) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
}
//made by shun 20220719

 I - Array Negations

You are given an array aa of nn integers, and an integer kk. You have to make kk negation operations such that at each operation you need to choose an element a_iai​ from the array and replace it with -a_i−ai​.

Your task is to find the optimal way to make the kk negation operations such that at the end the sum of the array aa is as maximal as possible. Can you?

Input

The first line contains an integer TT (1 \le T \le 1001≤T≤100) specifying the number of test cases.

The first line of each test case contains two integers nn and kk (1 \le n \le 10^41≤n≤104, 0 \le k \le 10^40≤k≤104), in which nn is the size of the array, and kk is the number of negation operations to be made.

Then a line follows contains nn integers a_1, \cdots, a_na1​,⋯,an​ (-100 \le a_i \le 100−100≤ai​≤100), giving the array aa.

Output

For each test case, print a single line containing the maximum sum of array aa after making the required number of negation operations.

Sample 1

InputcopyOutputcopy
3
3 1
4 6 2
4 2
-1 0 2 1
5 2
1 7 -4 2 -3
8
4
17

Note

In the first test case, the optimal way is to make the negation operation on a_3a3​. After this, the array will be = [4, 6, -2][4,6,−2], and its sum is 88.

第一个开的题,表面看起来很简单其实还是需要一点思考,警示自己不能自以为是,尽量一次读题就把细节把握到位。

问题就是分两种情况,当k小于n的时候直接sort然后取反就ok但是当k>n时候就代表所有负数都取正数,之后需要取最小正数取反

#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
signed main(){
	IOS;
	int a[N];
	int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        int num=0;
        for(int i=0; i<n; i++){
            cin>>a[i];
            if(a[i]<0) num++;
        }
        if(num<=k){
            int minn=0x3f3f3f3f;
            int x=-1;
            for(int i=0; i<n; i++){
                if(a[i]<0){
                    a[i]=-a[i];
                }
                if(a[i]<minn){
                    minn=a[i];
                    x=i;
                }
            }
            if((k-num)%2!=0){
                a[x]=-a[x];
            }
        }
        else{
            sort(a,a+n);
            for(int i=0; i<k; i++)
                a[i]=-a[i];
        }
        int sum=0;
        for(int i=0; i<n; i++)
        sum+=a[i];
        cout<<sum<<endl;
    }
}
//made by shun 20220719

J - Grid Beauty

You are given a grid gg consisting of nn rows each of which is divided into mm columns.

You can swap any two integers in the same row infinitely, but you cannot swap two integers from two different rows.

Your task is to maximize the beauty of the grid by rearranging integers in each row. The beauty of the grid is the number of pairs (i,\,ji,j) (1 < i \le n,\, 1 \le j \le m1<i≤n,1≤j≤m) such that g_{i,j}gi,j​ is equal to g_{i-1,j}gi−1,j​ (i.e. g_{i,j} \equiv g_{i - 1,j}gi,j​≡gi−1,j​).

Input

The first line contains an integer TT (1 \le T \le 51≤T≤5) specifying the number of test cases.

The first line of each test case contains two integers nn and mm (1 \le n, m \le 10^31≤n,m≤103), giving the number of rows and columns in the grid, respectively.

Then nn lines follow, each line contains mm integers, giving the grid. All values in the grid are between 11 and 10^8108 (inclusive).

Output

For each test case, print a single line containing the beauty of the grid.

Sample 1

InputcopyOutputcopy
2
2 3
1 2 3
4 1 2
3 3
5 7 9
3 2 9
5 3 2
2
3

Note

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Sponsor

这个题单独开了一个题解blog,学会多种方法解题,这道题首先想到了贪心+map记忆化,和班里零acm基础的同学学了一手二分做法,要学的还有太多了

//贪心做法
/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 16*/
#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
signed main(){
	IOS;
	int t;
	cin>>t;
	int n,m;
	while(t--){
		cin>>n>>m;
		int num;
		int sum=0;
		int res=0;
		map<int,int> mp1,mp2;
		for(int i=0;i<m;i++){
			cin>>num;
			mp1[num]++;
		}
		for(int i=1;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>num;
				if(!res) mp2[num]++;
				else mp1[num]++;
				if(!res){
					if(mp1.count(num)&&mp1[num]!=0) sum++,mp1[num]--;
				}
				else{
					if(mp2.count(num)&&mp2[num]!=0) sum++,mp2[num]--;
				}
			}
			if(!res){
				res=1;
				mp1.clear();
			}
			else{
				res=0;
				mp2.clear();
			}
		}
		cout<<sum<<endl;
	}
}
//made by shun 20220719



//二分做法
/*莫道桑榆晚,为霞尚满天*/

#include<bits/stdc++.h>

using namespace std;
const int N =1e3+5;
#define mem memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int,int> PII;
ll bsearch_1(ll a[],ll x,ll l, ll r)
{
    while (l < r)
    {
        ll mid = (l + r )>> 1;
        if (a[mid]>=x) r = mid;
        else l = mid + 1;
    }
    return l;
}
ll a[N][N];
int main()
{
    ll u;
    scanf("%lld",&u);
    while(u--)
    {
        ll n,m;
        scanf("%lld %lld",&n,&m);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
            a[i][j]=0;
        }
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<m;j++)
            {
                scanf("%lld",&a[i][j]);
            }
            sort(a[i],a[i]+m);
        }
        ll k=0;
        for(ll i=1;i<n;i++)
        {
            for(ll j=0;j<m;j++)
            {
                ll t=a[i][j];
                ll l=bsearch_1(a[i-1],t,0,m-1);
                if(a[i-1][l]==t)
                {
                    a[i-1][l]=-1;
                    k++;
                }
            }
        }
        printf("%lld\n",k);
    }
    return 0;
}

总结:1.总是自以为是wa掉一发再注意到细节,这个特别不好,之所以为了快速解题就是为了减少罚时得到更高的位次,然而wa掉一发之后罚时高了也影响节奏。

2.多学习,赛后对比自己和别人的思维,多一种思维就多一种方法

3.代码还是写的不够干净,敲码速度也需要提升

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值