2022杭电联赛2补题

目录

1002 C++ to Python(7151)

1007 Snatch Groceries(7156)

1009 ShuanQ(7158)

1012 Luxury cruise ship(7161)

1002 C++ to Python(7151)

Problem Description

Kayzin's repository has lots of code in C++, but Aba aba can only understand Python code. So Aba aba calls you for help.

The only things you should do is using (...) in Python to match std::make_tuple(...) in C++.

Input

First line has one integer T, indicates T test cases. In each case:

First line has string s, indicates C++ code.

The C++ code only contains std::make_tuple, "(", ")", "," and integers, without space.

1≤T≤100, the length of s not exceeds 1000.

Output

Each test cases print one line of Python code, do not print any space.

Sample Input

2 
std::make_tuple(-2,3,3,std::make_tuple(3,3)) 
std::make_tuple(std::make_tuple(1,1,4,5,1,4))

Sample Output

(-2,3,3,(3,3)) ((1,1,4,5,1,4))

#include
using namespace std;
int t;
char s[1001];
int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%s",s);
		for(int i=0;s[i];i++){
			if(s[i]!=':'&&s[i]!='_'&&!isalpha(s[i]))
			    putchar(s[i]);
		}printf("\n");
	}
	return 0;
}

1007 Snatch Groceries(7156)

Problem Description

TL;DR: Spanner implements snapshot isolation across data centers in this way. It uses the clock’s confidence interval as reported by the TrueTime API, and is based on the following observation: if you have two confidence intervals, each consisting of an earliest and latest possible timestamp (A=[Aearliest,Alatest],B=[Bearliest,Blatest]), and those two intervals do not overlap (i.e.,Aearliest
Now we use Spanner as a solution, there are millions of people snatching groceries, and everyone is given the clock’s confidence interval. The server executes each request in chronological order, and terminate in case of intervals overlap. Here is the question, how many people can get their food before the server is terminated.

Input

First line has one integer T, indicating there are T test cases. In each case:

First line has one integers n, indicating there are n people.

For next n lines, each line has 2 integers earliest,latest, indicates the clock’s confidence interval.

T≤10,1≤n≤105,0≤earliesti

Output

In each case, print one integer, indicates the answer.

Sample Input

2
3
1 2
3 4
5 6
3
1 2
2 3
1 5

Sample Output

3

0

#include
#include
using namespace std;
int t, n, e, l, ans = 0,x=0,y=0;
struct tim{
    int x,y;
    bool operator<(const tim A) {
        return x=a[i + 1].x&&i!=n)
             	break;
            else ans++;
        }printf("%d\n", ans);
    }
	return 0;
}
#include
using namespace std;
vector >a;
int t,n;
int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		a.resize(n+1);
		for(int i=0;i=a[i+1].first)break;
			ans++;
		}printf("%d\n",ans);
	}
	return 0; 
}

反思:1.每个样例需重置a[]

           2.排序后只需比较a[i].y

           3.用vector套pair不用手写比较函数,可直接sort

1009 ShuanQ(7158)

Problem Description

CX is a programmer of a mooc company. A few days ago, he took the blame for leakage of users' data. As a result, he has to develop an encryption algorithm, here is his genius idea.

First, the protocol specifies a prime modulus M, then the server generates a private key P, and sends the client a public key Q. Here Q=P−1,P×Q≡1modM.

Encryption formula: encrypted_data=raw_data×PmodM

Decryption formula: raw_data=encrypted_data×QmodM

It do make sense, however, as a master of number theory, you are going to decrypt it.You have intercepted information about P,Q,encrypted_data, and M keeps unknown. If you can decrypt it, output raw_data, else, say "shuanQ" to CX.

Input

First line has one integer T(T≤20), indicating there are T test cases. In each case:

One line has three integers P,Q,encrypted_data. (1
It's guaranteed that P,Q,encrypted_data

Output

In each case, print an integer raw_data, or a string "shuanQ".

Sample Input

2 
5 5 5 
6 6 6

Sample Output

shuanQ

1

#include
using namespace std;
int t,p,q,ed;
typedef long long ll;
ll rd,km,m;
int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&p,&q,&ed);
		km=1ll*p*q-1,rd=-1,m=0;
		for(ll i=2;i*i<=km;i++){
			if(km%i)continue;
			while(km%i==0)km/=i;
			m=i;
		}if(km>1)m=km;
		if(p

反思:1:1ll*格式化

1012 Luxury cruise ship(7161)

Problem Description

Kayzin plans to buy a luxury cruise ship which will cost N coins. But Kayzin didn't have enough coins, so Kayzin decided to start saving coins.

Kayzin will put 7 or 31 or 365 coins into his piggy bank each day. Now Kayzin wants to know at least how many days he will need to "exactly"(No more and no less) scrape together the money for buying a luxury cruise.

If Kayzin can't "exactly" scrape together the money for buying a luxury cruise, print -1.

Input

The first line contains an integer T(T≤1000) . Then T test cases follow.

For one case, the first line contains an integer N (1≤N≤1018) , N denotes the coins that a luxury cruise ship will cost.

Output

Print an integer for each case, indicating the minimum number of days Kayzin needs.

If Kayzin can't "exactly" scrape together the money for buying a luxury cruise, print -1.

Sample Input

5 
14 
38  
55 
403 
996

Sample Output

2 2 -1 3 16

佬的讲解:

2555既是7的倍数,又是365的倍数
对于一个n,我们可以考虑把它分成三部分
一部分由365解决,一部分由31解决,一部分由7解决
显然,尽量用365解决,答案会更优
如果用7解决的部分大于了2555,那我们就完全可以把其中的2555拿过来用365解决对吧
所以,思考一下,是不是n大于2555时,我们只需要考虑每天拿365就行了
这样就得出大规模数据的解法了:
在n大于2555时,我们每天都拿365,直到n小于2555,这时候我们只需要用上面的三重循环枚举
这其实就是题解里面所谓的“n较大时,用365将n缩小到背包的范围内”

#include
using namespace std;
typedef long long ll;
int t,b[3]={7,31,365},m=2555;
ll a,ans,Max=1234567890,f[2557];
int main()
{
	//memset(f,1234567890,sizeof(f));//错 
	for(int i=0;i<2557;i++)f[i]=Max;
	f[0]=0;
	for(int i=0;i<3;i++){
		for(int j=0;j+b[i]<=m;j++){
			if(f[j]!=Max)
			    f[j+b[i]]=min(f[j+b[i]],f[j]+1);
		}
	}
	scanf("%d",&t);
	while(t--){
		ans=0;
		scanf("%lld",&a);
		if(a>m){
			ans=(a-m)/365;//向上取整 
			if((a-m)%365)ans++;
			a-=ans*365;
		}if(f[a]!=Max)
		printf("%lld\n",ans+f[a]);
		else printf("-1\n");
	}
	return 0;
}

还找到一种方法:直接算出7*31*365内的值,比起大的缩小范围

2022“杭电杯”中国大学生算法设计超级联赛 (2) 杭电多校第二场_Nebula_xuan的博客-CSDN博客

你可能感兴趣的:(c++)