A. Got Any Grapes?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
For simplicity, we'll assume that there are only three types of grapes: green grapes, purple grapes and black grapes.
Andrew, Dmitry and Michal are all grapes' lovers, however their preferences of grapes are different. To make all of them happy, the following should happen:
- Andrew, Dmitry and Michal should eat at least xx, yy and zz grapes, respectively.
- Andrew has an extreme affinity for green grapes, thus he will eat green grapes and green grapes only.
- On the other hand, Dmitry is not a fan of black grapes — any types of grapes except black would do for him. In other words, Dmitry can eat green and purple grapes.
- Michal has a common taste — he enjoys grapes in general and will be pleased with any types of grapes, as long as the quantity is sufficient.
Knowing that his friends are so fond of grapes, Aki decided to host a grape party with them. He has prepared a box with aa green grapes, bbpurple grapes and cc black grapes.
However, Aki isn't sure if the box he prepared contains enough grapes to make everyone happy. Can you please find out whether it's possible to distribute grapes so that everyone is happy or Aki has to buy some more grapes?
It is not required to distribute all the grapes, so it's possible that some of them will remain unused.
Input
The first line contains three integers xx, yy and zz (1≤x,y,z≤1051≤x,y,z≤105) — the number of grapes Andrew, Dmitry and Michal want to eat.
The second line contains three integers aa, bb, cc (1≤a,b,c≤1051≤a,b,c≤105) — the number of green, purple and black grapes in the box.
Output
If there is a grape distribution that allows everyone to be happy, print "YES", otherwise print "NO".
Examples
input
Copy
1 6 2
4 3 3
output
Copy
YES
input
Copy
5 1 1
4 3 2
output
Copy
NO
Note
In the first example, there is only one possible distribution:
Andrew should take 11 green grape, Dmitry should take 33 remaining green grapes and 33 purple grapes, and Michal will take 22 out of 33available black grapes.
In the second test, there is no possible distribution, since Andrew is not be able to eat enough green grapes. :(
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y,z;
int a,b,c;
cin>>x>>y>>z>>a>>b>>c;
if(a<x)
{
printf("NO");
return 0;
}
else
{
a-=x;
}
int sum=a+b;
if(sum<y)
{
printf("NO");
return 0;
}
else sum-=y;
sum+=c;
if(sum<z)
{
printf("NO");
return 0;
}
printf("YES");
return 0;
}
B. Yet Another Array Partitioning Task
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
An array bb is called to be a subarray of aa if it forms a continuous subsequence of aa, that is, if it is equal to alal, al+1al+1, ……, arar for some l,rl,r.
Suppose mm is some known constant. For any array, having mm or more elements, let's define it's beauty as the sum of mm largest elements of that array. For example:
- For array x=[4,3,1,5,2]x=[4,3,1,5,2] and m=3m=3, the 33 largest elements of xx are 55, 44 and 33, so the beauty of xx is 5+4+3=125+4+3=12.
- For array x=[10,10,10]x=[10,10,10] and m=2m=2, the beauty of xx is 10+10=2010+10=20.
You are given an array a1,a2,…,ana1,a2,…,an, the value of the said constant mm and an integer kk. Your need to split the array aa into exactly kksubarrays such that:
- Each element from aa belongs to exactly one subarray.
- Each subarray has at least mm elements.
- The sum of all beauties of kk subarrays is maximum possible.
Input
The first line contains three integers nn, mm and kk (2≤n≤2⋅1052≤n≤2⋅105, 1≤m1≤m, 2≤k2≤k, m⋅k≤nm⋅k≤n) — the number of elements in aa, the constant mm in the definition of beauty and the number of subarrays to split to.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).
Output
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print k−1k−1 integers p1,p2,…,pk−1p1,p2,…,pk−1 (1≤p1<p2<…<pk−1<n1≤p1<p2<…<pk−1<n) representing the partition of the array, in which:
- All elements with indices from 11 to p1p1 belong to the first subarray.
- All elements with indices from p1+1p1+1 to p2p2 belong to the second subarray.
- …….
- All elements with indices from pk−1+1pk−1+1 to nn belong to the last, kk-th subarray.
If there are several optimal partitions, print any of them.
Examples
input
Copy
9 2 3
5 2 5 2 4 1 1 3 2
output
Copy
21
3 5
input
Copy
6 1 4
4 1 3 2 2 3
output
Copy
12
1 3 5
input
Copy
2 1 2
-1000000000 1000000000
output
Copy
0
1
Note
In the first example, one of the optimal partitions is [5,2,5][5,2,5], [2,4][2,4], [1,1,3,2][1,1,3,2].
- The beauty of the subarray [5,2,5][5,2,5] is 5+5=105+5=10.
- The beauty of the subarray [2,4][2,4] is 2+4=62+4=6.
- The beauty of the subarray [1,1,3,2][1,1,3,2] is 3+2=53+2=5.
The sum of their beauties is 10+6+5=2110+6+5=21.
In the second example, one optimal partition is [4][4], [1,3][1,3], [2,2][2,2], [3][3].
这题我差点把正确的方法忽略了,我一开始就相=想着排序将前m*k标记,然后再=在原数组一遍for找m个标记即可,不知道为什么被自己否定了,想其他方法wa了一发后才想起这种方法好像也行。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
int n,m,k;
struct node
{
ll val;
int id;
}a[N];
bool vis[N];
ll b[N];
bool cmp(node a,node b)
{
if(a.val>b.val) return 1;
return 0;
}
int main()
{
cin>>n>>m>>k;
ll sum=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i].val);
b[i]=a[i].val;
sum+=a[i].val;
a[i].id=i;
}
/*if(m*k==n)
{
printf("%lld\n",sum);
return 0;
}*/
sort(a+1,a+1+n,cmp);
for(int i=1;i<=m*k;i++) vis[a[i].id]=1;
int num=0;
ll ans=0;
vector<int>G;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
num++;
ans+=b[i];
// printf("ans:%lld\n",ans);
}
if(num==m)
{
num=0;
G.push_back(i);
}
}
printf("%lld\n",ans);
for(int i=0;i<G.size()-1;i++)
printf("%d ",G[i]);
}
C. Trailing Loves (or L'oeufs?)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis.
Aki is fond of numbers, especially those with trailing zeros. For example, the number 92009200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.
However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.
Given two integers nn and bb (in decimal notation), your task is to calculate the number of trailing zero digits in the bb-ary (in the base/radix of bb) representation of n!n! (factorial of nn).
Input
The only line of the input contains two integers nn and bb (1≤n≤10181≤n≤1018, 2≤b≤10122≤b≤1012).
Output
Print an only integer — the number of trailing zero digits in the bb-ary representation of n!n!
Examples
input
Copy
6 9
output
Copy
1
input
Copy
38 11
output
Copy
3
input
Copy
5 2
output
Copy
3
input
Copy
5 10
output
Copy
1
Note
In the first example, 6!(10)=720(10)=880(9)6!(10)=720(10)=880(9).
In the third and fourth example, 5!(10)=120(10)=1111000(2)5!(10)=120(10)=1111000(2).
The representation of the number xx in the bb-ary base is d1,d2,…,dkd1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0x=d1bk−1+d2bk−2+…+dkb0, where didi are integers and 0≤di≤b−10≤di≤b−1. For example, the number 720720 from the first example is represented as 880(9)880(9) since 720=8⋅92+8⋅9+0⋅1720=8⋅92+8⋅9+0⋅1.
You can read more about bases here.
这题是真不会,补的,好像涉及了什么唯一分解定理
题解:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
using namespace std;
const int MAXN = 2e6+1000;
LL N, B;
vector<LL>prime;
map<LL, int>mmp;
void get_p(LL n)
{
LL len = sqrt((double)n);
for(LL i = 2; i <= len; i++){
if(n%i == 0){
prime.push_back(i);//prime数里存的是能被n整数的数i,i是不是素数呢?
//答案是的。仔细想想。。 因为经历2,把2的倍数全除没了,然后经历3,经历5,一直素数
while(n%i == 0){
n/=i;
mmp[i]++;//保存了i多少次方
}
}
}
if(n != 1){
prime.push_back(n);
mmp[n]++;
}
}
LL calc(LL n, LL p)
{
LL res = 0;
while(n){//等于 [ n/p ] + [ n/(p^2) ] + [ n/(p^3) ] ,累除法,自己好好想想
res += n/p;
n/=p;
}
return res;
}
int main()
{
LL sum = 1LL;
scanf("%I64d %I64d", &N, &B);
get_p(B);
LL ans = (1LL<<62);
for(LL i = 0; i < prime.size(); i++){
ans = min(ans, calc(N, prime[i])/mmp[prime[i]]);
}
printf("%I64d\n", ans);
return 0;
}