设为首页 收藏本站
查看: 849|回复: 0

[经验分享] May Challenge 2015(CodeChef 2015年5月月赛)

[复制链接]

尚未签到

发表于 2015-11-26 08:15:31 | 显示全部楼层 |阅读模式

                                               Chef and new recipe



Rupsa recently started to intern under Chef. He gave her N type of ingredients of varying quantity A1, A2, ..., AN respectively to store it. But as she is
lazy to arrange them she puts them all in a storage box.


Chef comes up with a new recipe and decides to prepare it. He asks Rupsa to get two units of each type ingredient for the dish. But when she went to retrieve the ingredients, she realizes that she can only pick one item at a time from
the box and can know its type only after she has picked it out. The picked item is not put back in the bag.


She, being lazy, wants to know the maximum number of times she would need to pick items from the box in the worst case so that it is guaranteed that she gets at least two units of each type of ingredient. If it is impossible to pick items in such a way, print
-1.



Input



  • The first line of the input contains an integer T denoting the number of test cases.
  • The first line of each test case contains a single integer N denoting the number of different type of ingredients.
  • The second line contains N space-separated integers A1, A2, ..., AN denoting the
    quantity of each ingredient.


Output



  • For each test case, output a single line containing an integer denoting the answer corresponding to that test case.


Constraints



  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ Ai ≤ 104


Sub tasks



  • Subtask #1: 1 ≤ N ≤ 1000 (30 points)
  • Subtask #2: original constraints (70 points)


Example


Input:
2
2
2 2
1
6
Output:
4
2


Explanation



  • In Example 1, she need to pick up all items.
  • In Example 2, since there is only one type of ingredient, picking two items is enough.



题目大意
    一个罐子里有n个食材,每个食材取2单位,每次他从罐子内取1单位,求最多需多少次?

题解:
     让最少的食材最后取就可以了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf=0x7fffffff;
int main()
{
int t;
scanf(&quot;%d&quot;,&t);
while(t--)
{
int sum=0,minx=inf,n;
scanf(&quot;%d&quot;,&n);
int x;
for(int i=0;i<n;i++)
{
scanf(&quot;%d&quot;,&x);
if(x<minx)
minx=x;
sum+=x;
}
if(minx<2)
printf(&quot;-1\n&quot;);
else
printf(&quot;%d\n&quot;,sum-minx+2);
}
return 0;
}







                                                Set Difference



Churu is working as a data scientist in Coderpur. He works on a lot of data on the daily basis. One day, he found an interesting problem, which was very easy to solve for small data but was getting more complex with increasing number
of data points. So, Churu needs your help in solving this problem.


Given a set S of N non-negative integers (Some integers might occur more than once in the set), find out the value of SETDIFF(S).




Where max(s) represents the maximum value in set s whereas min(s) represents the minimum value in the set s.

As value of SETDIFF(S) can be very large, print it modulo (109 &#43; 7) .


There might be repeated values in the set. For set S = {1,2,2}, consider that first 2 is not same as the second 2 and there will be two different subsets {1,2}. See
last sample case for the more clarifications.



Input



  • First line of input contains an integer T denoting number of test cases.
  • For each test case, first line will contain an integer N denoting number of elements in set S.
  • Next line contains N space separated integers denoting the set S.


Output



For each test case, print a single integer representing the answer of that test case.



Note



Two subsets will be called different if there exists an index i such that S occurs in one of the subset and not in another.



Constraints



Subtask #1: 20 points



  • 1 ≤ T ≤ 5, 1 ≤ N ≤ 1000, 0 ≤ value in set ≤ 109


Subtask #2: 25 points



  • 1 ≤ T ≤ 5, 1 ≤ N ≤ 105, 0 ≤ value in set ≤ 1000


Subtask #3: 55 points



  • 1 ≤ T ≤ 5, 1 ≤ N ≤ 105, 0 ≤ value in set ≤ 109


Example


Input:
4
2
1 2
3
1 2 3
4
1 2 3 4
3
1 2 2
Output:
1
6
23
3


Explanation



For first case answer will be 2-1 = 1.


For the second case:



Subset = {1}, max(s)-min(s) = 0.

Subset = {2}, max(s)-min(s) = 0.

Subset = {3}, max(s)-min(s) = 0.

Subset = {1,2}, max(s)-min(s) = 1.

Subset = {2,3}, max(s)-min(s) = 1.

Subset = {1,3}, max(s)-min(s) = 2.

Subset = {1,2,3}, max(s)-min(s) = 2.

So the output will be 1&#43;1&#43;2&#43;2 = 6.


In the last case, there are three subsets, {1,2}, {1,2} and {1,2,2} having max(s) - min(s) = 1 for each.








题目大意
       s集合的所有子集的最大&#20540;与最小&#20540;差的和。



题解:
          排个序,枚举最大&#20540;,最小&#20540;,有k个比要枚举的数小的,就有2^k个子集,将最大&#20540;的和加起来减去最小&#20540;
的和就可以了。


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
const int mod=1e9+7;
long long p[maxn];
int a[maxn];
int main()
{
p[0]=1;
for(int i=1;i<maxn;i++)
p=(p[i-1]*2)%mod;
int t;
scanf(&quot;%d&quot;,&t);
while(t--)
{
int n;
scanf(&quot;%d&quot;,&n);
for(int i=0;i<n;i++)
scanf(&quot;%d&quot;,&a);
sort(a,a+n);
int ans=0;
for(int i=n-1;i>=0;i--)
{
ans=(ans+((long long)a*p)%mod)%mod;
}
for(int i=0;i<n;i++)
ans=(ans-((long long)a*p[n-i-1])%mod+mod)%mod;
printf(&quot;%d\n&quot;,ans);
}
return 0;
}




                                    Chef and Prime Divisors



You are given two positive integers – A and B. You have to check whether A is divisible by all the prime divisors of B.



Input



The first line of the input contains an integer T denoting the number of test cases. The description of Ttest cases follows.


For each test case, you are given two space separated integers – A and B.



Output



For each test case, output &quot;Yes&quot; (without quotes) if A contains all prime divisors of B, otherwise print &quot;No&quot;.



Constraints



  • 1 ≤ T ≤ 104
  • 1 ≤ A, B ≤ 1018


Subtasks



  • Subtask 1 (20 points):1 ≤ B ≤ 107
  • Subtask 2 (30 points):1 ≤ A ≤ 107
  • Subtask 3 (50 points): Original constraints


Example


Input:
3
120 75
128 16
7 8
Output:
Yes
Yes
No


Explanation



Example case 1. In the first case 120 = 23*3*5 and 75 = 3*52. 120 is divisible by both 3 and 5. Hence, we will print &quot;Yes&quot;


Example case 2. In the second case both 128 and 16 are powers of two. Hence, the answer is &quot;Yes&quot;


Example case 3. In the third case 8 is power of two and 7 is not divisible by 2. So, the answer is &quot;No&quot;






题目大意:
     给两个数A和B,判断A是否能被B的所有质因数整除。


题解:
     A和B的&#20540;较大,直接找B的所有质因数会超时,我们可以取两个的最大公约数,它们的公约数肯定是由B的质因数相乘得到的,且能被A整除,于是B就可以去除这部分质因数,B=B/gcd(A,B),直到A,B两个的公约数为1,如果此时B为1,A能被B的所有质因数整除,否则不能。






代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
long long gcd(long long a,long long b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
int t;
scanf(&quot;%d&quot;,&t);
while(t--)
{
long long n,m;
scanf(&quot;%lld%lld&quot;,&n,&m);
int sign=1;
if(n%m!=0)
{
while(1)
{
if(n%m==0)
break;
long long k=gcd(n,m);
if(k==1)
{
sign=0;
break;
}
else
{
while(m%k==0)
m=m/k;
}
}
}
if(sign)
printf(&quot;Yes\n&quot;);
else
printf(&quot;No\n&quot;);
}
}





                                     Devu and binary String





Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string
does not contain more than k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string.


As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.



Input



  • First line of input contains an integer T denoting the number of test cases.
  • For each test case, there are two lines.
  • First line contains two space separated integers n, k as defined in the problem.
  • Next line contains string s of size n.


Output



  • For each test case, print two lines.
  • First line should contain an integer corresponding to minimum number of operations Devu needs.
  • In second line, print one of the possible modified strings.


Constraints



Subtask #1: 20 points



  • 1 ≤ T ≤ 100, 1 ≤ n ≤ 20, 1 ≤ k ≤ n


Subtask #2: 35 points



  • 1 ≤ T ≤ 102, 1 ≤ n ≤ 103, 1 ≤ k ≤ n


Subtask #3: 45 points



  • 1 ≤ T ≤ 105, 1 ≤ n ≤ 105, 1 ≤ k ≤ n
  • Sum of n over all the test cases is ≤ 106


Example


Input:
3
2 1
11
2 2
11
4 1
1001
Output:
1
10
0
11
2
1010


Explanation



Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.


Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.


Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character).








题目大意:
    长度为n的01串,每次操作只能翻转一个字符,求最少操作数,使其不含超过k个连续的相同的字符。


题解:
    如果连续的长度超过k个了,这时就需要翻转了,到底要翻转哪个呢?这就需分类讨论下了,假如第k&#43;1个相同字符的位置是i,后是是与它相同的字符,这时只需翻转i位置最优,假如后面的与它不同,此时翻转当前位置可能导致后面的串长度超了,就不会最优了,此时就需要翻转i-1了,既将前面分成了不超过k的连续相同的串,又不会对后面的造成影响。
   还有,此策略对k==1的不适用,i-1会对前面的造成影响,k==1时的特殊考虑下就可以了,肯定是01相间的,取最小的就可以了。


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+1000;
int s[maxn];
int a[maxn];
int main()
{
int t;
scanf(&quot;%d&quot;,&t);
while(t--)
{
int n,k;
scanf(&quot;%d%d&quot;,&n,&k);
char temp;
getchar();
for(int i=0; i<n; i++)
{
scanf(&quot;%c&quot;,&temp);
s=temp-'0';
}
int cur=1;
int ans=0;
int cnt=s[0];
if(k!=1)
{
for(int i=1; i<n; i++)
{
if(s==cnt)
{
cur++;
}
else
{
cur=1;
cnt=s;
}
if(cur==k+1&&i<n-1&&s[i+1]==cnt)
{
s=!s;
cur=0;
ans++;
}
if(cur==k+1&&i<n-1&&s[i+1]!=cnt)
{
ans++;
s[i-1]=!s[i-1];
cur=1;
}
if(cur==k+1&&i==n-1)
{
ans++;
s=!s;
}
}
}
else
{
for(int i=1;i<n;i++)
{
if(s!=(!s[i-1]))
{
ans++;
s=!s[i-1];
}
}
}
if(n-ans>=ans)
{
printf(&quot;%d\n&quot;,ans);
for(int i=0; i<n; i++)
printf(&quot;%d&quot;,s);
printf(&quot;\n&quot;);
}
else
{
printf(&quot;%d\n&quot;,n-ans);
for(int i=0;i<n;i++)
printf(&quot;%d&quot;,!s);
printf(&quot;\n&quot;);
}
}
return 0;
}





                                          Chef and Cake



The Chef once decided to prepare some nice dishes on his birthday. There are N items kept on his shelf linearly from position 1 to N. Taste of the i-th item is denoted
by a integer Ai.


He wants to make Q dishes. A dish will be made using some ingredients in the continuous range AL, AL &#43; 1, , , AR (1-base indexing).
Quality of the dish will be determined by the ingredient with minimum taste.


Chef wants help of his assistant Rupsa to find out sum and product of qualities of the dishes. As product of the qualities of the dishes could be very large, print it modulo 109 &#43; 7. Also,
you are given an integerK and you are assured that for each dish, the size of continuous range of the ingredients (i.e. R - L &#43; 1) will always lie between K and 2 * K,
both inclusive.


Method of generation of Array A

You are given non-negative integer parameters a, b, c, d, e, f, r, s, t, m, A[1]



for x = 2 to N:
if(t^x mod s  <= r)        // Here t^x signifies &quot;t to the power of x&quot;
A[x] = (a*A[x-1]^2 &#43; b*A[x-1] &#43; c) mod m
else
A[x] = (d*A[x-1]^2 &#43; e*A[x-1] &#43; f) mod m


Method of generation of range of ingredients for Q dishes

You are given non-negative integer parameters L1, La, Lc, Lm, D1, Da, Dc, Dm



for i = 1 to Q:
L1 = (La * L1 &#43; Lc) mod Lm;
D1 = (Da * D1 &#43; Dc) mod Dm;
L = L1 &#43; 1;
R = min(L &#43; K - 1 &#43; D1, N);


Input



  • The first line contains three integers N, K and Q.
  • The second line contains the integers a, b, c, d, e, f, r, s, t, m, and A[1].
  • Then third line contains the integers L1, La, Lc, Lm, D1, Da, Dc, and Dm


Output



Output two space separated integers:



  • The sum of qualities of the dishes.
  • The product of qualities of the dishes modulo 109&#43;7.


Constraints



  • 1 ≤ N, Q ≤ 107
  • 1 ≤ K ≤ N
  • 0 ≤ a, b, c, d, e, f, r, s, t, m, A[1] ≤ 109&#43;7
  • 1 ≤ Lm ≤ N - K &#43; 1
  • 1 ≤ Dm ≤ K &#43; 1
  • 1 ≤ La, Lc ≤ Lm
  • 1 ≤ Da, Dc ≤ Dm
  • 1 ≤ L1 ≤ N
  • 1 ≤ D1 ≤ K


Sub tasks



  • Subtask #1: 1 ≤ N, Q ≤ 1000 (10 points)
  • Subtask #2: 1 ≤ Q ≤ 104 (20 points)
  • Subtask #3: original constraints (70 points)


Example


Input:
4 2 1
1 1 1 1 1 1 1 1 1 100 1
1 1 1 3 1 1 1 2
Output:
13 13


Explanation



  • The array A comes out to be {1, 3, 13, 83} and the first dish has L = 3 and R
    = 4. The minimum in this range is 13, thus the sum and product both are 13 and hence the answer.



Note



Multiplier for C# and Java have been reduced to 1.5 instead of 2.






题目大意:
     生成长度为n的数组,有Q个查询,查询[L,R]区间内的最小&#20540;,求这Q个查询的最小&#20540;的和与积,区间长度在k与2*k间。


题解:
    RMQ算法,由于区间长度在k与2*k间,维护一个区间长度为k的最小&#20540;,查询的时候就可以取[L,L&#43;k],[R-k&#43;1,R]两端区间的最小&#20540;,做到O(1)查询。


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e7+1000;
const int mod=1e9+7;
long long A[maxn];
int cnt[maxn];
int rmq[maxn];
int main()
{
int n,k,q;
while(~scanf(&quot;%d%d%d&quot;,&n,&k,&q))
{
long long a,b,c,d,e,f,r,s,t,m;
scanf(&quot;%lld%lld%lld%lld%lld%lld%lld%lld%lld%lld%lld&quot;,&a,&b,&c,&d,&e,&f,&r,&s,&t,&m,&A[1]);
long long cur=t;
long long L1,La,Lc,Lm,D1,Da,Dc,Dm;
int L,R;
scanf(&quot;%lld%lld%lld%lld%lld%lld%lld%lld&quot;,&L1,&La,&Lc,&Lm,&D1,&Da,&Dc,&Dm);
for(int i=2; i<=n; i++)
{
cur=(cur*t)%s;
if(cur<=r)
A = ((a*(A[i-1]*A[i-1])%m)%m + (b*A[i-1]%m) + c) % m;
else
A = ((d*(A[i-1]*A[i-1])%m)%m + (e*A[i-1])%m + f) % m;
}
int front=1,rear=0;
for(int i=1;i<=n;i++)
{
while(front<=rear&&i-cnt[front]+1>k)
++front;
while(front<=rear&&A<=A[cnt[rear]])
--rear;
cnt[++rear]=i;
if(i>=k)
rmq[i-k+1]=A[cnt[front]];
}
long long ans=1,sum=0;
long long temp;
for(int i=0; i<q; i++)
{
L1 = ((La * L1)%Lm + Lc) % Lm;
D1 = ((Da * D1)%Dm + Dc) % Dm;
L =(int)L1 + 1;
R = min(L + k - 1 + (int)D1, n);
temp=min(rmq[L],rmq[R-k+1]);
sum+=temp;
ans=(ans*temp)%mod;
}
printf(&quot;%lld %lld\n&quot;,sum,ans);
}
return 0;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143606-1-1.html 上篇帖子: UrbanCode Deploy 和Chef的对比以及结合 下篇帖子: codechef Chef and Left-Right
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表