CF基础数学题

CF基础数学题

B. Divisors of Two Integers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently you have received two positive integer numbers x
and y. You forgot them, but you remembered a shuffled list containing all divisors of x (including 1 and x) and all divisors of y (including 1 and y). If d is a divisor of both numbers x and y at the same time, there are two occurrences of d

in the list.

For example, if x=4
and y=6 then the given list can be any permutation of the list [1,2,4,1,2,3,6]. Some of the possible lists are: [1,1,2,4,6,3,2], [4,6,1,1,2,3,2] or [1,6,3,2,4,1,2]

.

Your problem is to restore suitable positive integer numbers x
and y

that would yield the same list of divisors (possibly in different order).

It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers x
and y

.
Input

The first line contains one integer n
(2≤n≤128) — the number of divisors of x and y

.

The second line of the input contains n
integers d1,d2,…,dn (1≤di≤104), where di is either divisor of x or divisor of y. If a number is divisor of both numbers x and y

then there are two copies of this number in the list.
Output

Print two positive integer numbers x
and y

— such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.
Example
Input
Copy

10
10 2 8 1 2 4 1 20 4 5

Output
Copy

20 8


找出最大的那个数(所求的一个数),遍历把他的因子去掉那么剩下来的最大的数就是所求的另一个数。

#include<bits/stdc++.h>
using namespace std;
int a[130];
int main()
{
	int n;
	scanf("%d",&n);
	int maxx=0;
	unordered_map<int ,int >mp;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		mp[a[i]]++;
		maxx=max(a[i],maxx);
	}
	int maxx2=0;
	for(int i=0;i<n;i++)
	{
		if(maxx%a[i]==0)
		{
			mp[a[i]]--;
		}
		if(mp[a[i]]>0)
		{
			maxx2=max(maxx2,a[i]);
		}
	}
	printf("%d %d\n",maxx,maxx2);
 } 

A. Ksusha and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ksusha is a beginner coder. Today she starts studying arrays. She has array a1, a2, …, an, consisting of n positive integers.

Her university teacher gave her a task. Find such number in the array, that all array elements are divisible by it. Help her and find the number!
Input

The first line contains integer n (1 ≤ n ≤ 105), showing how many numbers the array has. The next line contains integers a1, a2, …, an (1 ≤ ai ≤ 109) — the array elements.
Output

Print a single integer — the number from the array, such that all array elements are divisible by it. If such number doesn’t exist, print -1.

If there are multiple answers, you are allowed to print any of them.
Examples
Input
Copy

3
2 2 4

Output
Copy

2

Input
Copy

5
2 1 3 1 6

Output
Copy

1

Input
Copy

3
2 3 5

Output
Copy

-1

#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
	int n;
	scanf("%d",&n);
	int ans=-1;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		
	}
	sort(a,a+n);
	if(a[0]==1) 
	{
		printf("1\n");
		return 0; 
	} 
	int ff=1;
	for(int i=1;i<n;i++)
	{
		if(a[i]%a[0]!=0)
		{
			printf("-1");
			ff=0;
			break;
		}
	}
	if(ff)
	{
		printf("%d",a[0]);
	}
	
 } 
B. Square Difference
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice has a lovely piece of cloth. It has the shape of a square with a side of length a
centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b<a

). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).

Alice would like to know whether the area of her cloth expressed in square centimeters is prime. Could you help her to determine it?
Input

The first line contains a number t
 (1≤t≤5

) — the number of test cases.

Each of the next t
lines describes the i-th test case. It contains two integers a and b (1≤b<a≤1011)

 — the side length of Alice's square and the side length of the square that Bob wants.
Output

Print t
lines, where the i-th line is the answer to the i

-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".

You can print each letter in an arbitrary case (upper or lower).
Example
Input
Copy

4
6 5
16 13
61690850361 24777622630
34 33

Output
Copy

YES
NO
NO
YES

Note

The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 62−52=36−25=11

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool isprime(ll x)
{
	if(x==1) return 0;
	for(int i=2;i<=sqrt(x);i++)
	{
		if(x%i==0)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{
		ll a,b;
		scanf("%lld%lld",&a,&b);
		if(a-b!=1) 
		{
			printf("NO\n");
			continue;
		}
		ll ans=a+b;
		if(isprime(ans)) printf("Yes\n");
		else printf("NO\n");
	}
}

似乎时间复杂度更小的方法(适用对于范围在int内的数)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool isprime(ll x)
{
	if(x==2||x==3||x==5) return 1;
	if(x%2==0||x%3==0||x%5==0||x==1) return 0;
	ll c=7,a[8]={4,2,4,2,4,6,2,6};
	while(c*c<=x)
	{
		for(auto i:a)
		{
			if(x%c==0)
			{
				return 0;
			}
			c+=i;
		}
	}
	return 1;
}
int main()
{
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{
		ll a,b;
		scanf("%lld%lld",&a,&b);
		if(a-b!=1) 
		{
			printf("NO\n");
			continue;
		}
		ll ans=a+b;
		if(isprime(ans)) printf("Yes\n");
		else printf("NO\n");
	}
}

A. I’m bored with life
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It’s well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vičkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vičkopolis. He almost even fell into a depression from boredom!

Leha came up with a task for himself to relax a little. He chooses two integers A and B and then calculates the greatest common divisor of integers “A factorial” and “B factorial”. Formally the hacker wants to find out GCD(A!, B!). It’s well known that the factorial of an integer x is a product of all positive integers less than or equal to x. Thus x! = 1·2·3·…·(x - 1)·x. For example 4! = 1·2·3·4 = 24. Recall that GCD(x, y) is the largest positive integer q that divides (without a remainder) both x and y.

Leha has learned how to solve this task very effective. You are able to cope with it not worse, aren’t you?
Input

The first and single line contains two integers A and B (1 ≤ A, B ≤ 109, min(A, B) ≤ 12).
Output

Print a single integer denoting the greatest common divisor of integers A! and B!.
Example
Input
Copy

4 3

Output
Copy

6

Note

Consider the sample.

4! = 1·2·3·4 = 24. 3! = 1·2·3 = 6. The greatest common divisor of integers 24 and 6 is exactly 6.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int n,m;
	cin>>n>>m;
	ll ans=1;
	for(int i=2;i<=min(n,m);i++)
	{
		ans*=i;
	}
	printf("%lld",ans);
}

组合数

C. The World is a Theatre
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n boys and m girls attending a theatre club. To set a play “The Big Bang Theory”, they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different.

Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java.
Input

The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m).
Output

Find the required number of ways.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
Examples
Input
Copy

5 2 5

Output
Copy

10

Input
Copy

4 3 5

Output
Copy

3

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll  get_c( int n,int  m)
{
    ll c[100][100];
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=m;j++)
		{
			if(j==0||j==i)
			{
				c[i][j]=1;
			}else 
			{
				c[i][j]=c[i-1][j-1]+c[i-1][j];
			}
		 } 
	}
	return c[n][m];
}

int main()
{
	int  n,m,t;
	cin>>n>>m>>t;
	ll ans=0;
	for(int  i=4;i<=t-1;i++)
	{
		ll a=get_c(n,i);
		
			ans+=a*get_c(m,t-i);
	}
	cout<<ans<<endl;
}

B. Proper Nutrition
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has n burles. One bottle of Ber-Cola costs a burles and one Bars bar costs b burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars.

Find out if it’s possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly n burles.

In other words, you should find two non-negative integers x and y such that Vasya can buy x bottles of Ber-Cola and y Bars bars and x·a + y·b = n or tell that it’s impossible.
Input

First line contains single integer n (1 ≤ n ≤ 10 000 000) — amount of money, that Vasya has.

Second line contains single integer a (1 ≤ a ≤ 10 000 000) — cost of one bottle of Ber-Cola.

Third line contains single integer b (1 ≤ b ≤ 10 000 000) — cost of one Bars bar.
Output

If Vasya can’t buy Bars and Ber-Cola in such a way to spend exactly n burles print «NO» (without quotes).

Otherwise in first line print «YES» (without quotes). In second line print two non-negative integers x and y — number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly n burles, i.e. x·a + y·b = n. If there are multiple answers print any of them.

Any of numbers x and y can be equal 0.
Examples
Input
Copy

7
2
3

Output
Copy

YES
2 1

Input
Copy

100
25
10

Output
Copy

YES
0 10

Input
Copy

15
4
8

Output
Copy

NO

Input
Copy

9960594
2551
2557

Output
Copy

YES
1951 1949

Note

In first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2·2 + 1·3 = 7 burles.

In second example Vasya can spend exactly n burles multiple ways:

buy two bottles of Ber-Cola and five Bars bars;
buy four bottles of Ber-Cola and don't buy Bars bars;
don't buy Ber-Cola and buy 10 Bars bars. 

In third example it’s impossible to but Ber-Cola and Bars bars in order to spend exactly n burles.

水题

#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
int main()
{
	ll n,a,b;
	cin>>n>>a>>b;
	for(int i=0;i<=n/a;i++)
	{
		if((n-a*i)%b==0)
		{
			printf("YES\n");
			printf("%d %d\n",i,(n-a*i)/b);
			return 0;
		}
	}
	printf("NO\n");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值