Codeforces Round #191 (Div. 2)
来源:互联网 发布:js混淆怎么读 编辑:程序博客网 时间:2024/05/29 09:35
Iahub got bored, so he invented a game to be played on paper.
He writes n integers a1, a2, ..., an. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indicesi and j (1 ≤ i ≤ j ≤ n) and flips all valuesak for which their positions are in range[i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 1 -x.
The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.
The first line of the input contains an integer n (1 ≤ n ≤ 100). In the second line of the input there aren integers: a1, a2, ..., an. It is guaranteed that each of those n values is either 0 or 1.
Print an integer — the maximal number of 1s that can be obtained after exactly one move.
51 0 0 1 0
4
41 0 0 1
4
In the first case, flip the segment from 2 to 5 (i = 2, j = 5). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].
In the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1.
题目大意:
我们现在必须翻转一个区间一次,使得0->1 1->0.
问最多翻转之后的1的个数。
思路:
O(n^3)暴力
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[150];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d",&a[i]); int output=0; for(int L=1;L<=n;L++) { for(int R=L;R<=n;R++) { int tmp=0; for(int i=1;i<=n;i++) { if(i<=R&&i>=L) { tmp+=1-a[i]; } else tmp+=a[i]; } output=max(output,tmp); } } printf("%d\n",output); }}
Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting ofn integers.
A sequence a1,a2, ..., an, consisting of n integers, is Hungry if and only if:
- Its elements are in increasing order. That is an inequality ai < aj holds for any two indicesi, j (i < j).
- For any two indices i and j (i < j), aj must not be divisible by ai.
Iahub is in trouble, so he asks you for help. Find a Hungry sequence with n elements.
The input contains a single integer: n (1 ≤ n ≤ 105).
Output a line that contains n space-separated integersa1 a2, ..., an(1 ≤ ai ≤ 107), representing a possible Hungry sequence. Note, that eachai must not be greater than10000000 (107) and less than1.
If there are multiple solutions you can output any one.
3
2 9 15
5
11 14 20 27 31
题目大意:
让你构造出来一个长度为N的严格递增子序列,使得任意a【j】%a【i】>1(j>i);
思路:
预处理出来1e5个素数即可。
任意两个素数的gcd肯定都是1.
Ac代码:
#include<stdio.h>#include<string.h>#include<math.h>using namespace std;int Array[150000];int Is_or[1500000];void init(){ memset(Is_or,0,sizeof(Is_or)); Is_or[0]=1; Is_or[1]=1; int a,b; for(int j=2;j<sqrt(1500000);j++) { if(Is_or[j]==0) for(int k=j+j;k<=1500000;k+=j) { Is_or[k]=1; } } int cnt=0; for(int i=2;i<=1500000-1;i++) { if(Is_or[i]==0) { Array[cnt++]=i; } }}int main(){ init(); int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { printf("%d ",Array[i]); } printf("\n"); }}
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by5. Note that, the resulting number may contain leading zeros.
Now Iahub wants to count the number of ways he can obtain magic number, modulo1000000007 (109 + 7). Two ways are different, if the set of deleted positions ins differs.
Look at the input part of the statement, s is given in a special form.
In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integerk (1 ≤ k ≤ 109). The plates is formed by concatenating k copies of a together. That isn = |a|·k.
Print a single integer — the required number of ways modulo 1000000007 (109 + 7).
12561
4
139902
528
5552
63
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.
In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.
In the third case, except deleting all digits, any choice will do. Therefore there are26 - 1 = 63 possible ways to delete digits.
题目大意:
给你一个字符串,其真实字符串是给出的字符串重复K次的结果。
现在让你任意删除一些数字,使得剩余的部分是一个%5==0的数,允许有前导0.
问你有多少种删除的方式。
思路:
既然可以有前导0.
现在假设k==1的话,不难推出:Ans=Σ2^i(a【i】==0||a【i】==5);
那么此时K==2的话,也不难推出:Ans=Σ2^i(a【i】==0||a【i】==5)+2^len*Σ2^i(a【i】==0||a【i】==5)
那么此时K==3的话,也不难推出:Ans=Σ2^i(a【i】==0||a【i】==5)+2^len*Σ2^i(a【i】==0||a【i】==5)+2^(2*len)*Σ2^i(a【i】==0||a【i】==5)
那么我们设定Tmp=Σ2^i(a【i】==0||a【i】==5),那么最终的结果一定是:
Ans=Tmp*(1+2^len+2^(2*len)+2^(3*len)..................+2^((k-1)*len));
很显然,括号中的答案就是一个等比数列求和。
等比数列求和需要用到除法,所以我们需要使用逆元。
又因为数据比较大,我写的时候为了求稳同时使用了快速乘。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;#define ll __int64char a[1050000];const ll mod=1e9+7;ll kuaisucheng(ll a,ll b){ ll ans=0; while(b) { if(b&1) { ans=(ans+a); if(ans>=mod)ans-=mod; } a=(a+a); if(a>=mod)a-=mod; b/=2; } return ans;}ll kuaisumi(ll a,ll b){ a%=mod; ll ans=1; while(b) { if(b&1) { ans=kuaisucheng(ans,a); } a=kuaisucheng(a,a); b/=2; } return ans;}ll extend_euclid(ll a,ll b,ll &x,ll &y){ if(b==0) { x=1,y=0; return a; } else { ll r = extend_euclid(b,a%b,x,y); ll t = x; x = y; y = t - (a/b)*y; return r; }}ll chufaqumo(ll fenzi ,ll fenmu){ ll x,y; extend_euclid(fenmu,mod,x,y); x=(x%mod+mod)%mod; x=(x*fenzi+mod)%mod; return x;}int main(){ while(~scanf("%s",a+1)) { ll kk; scanf("%I64d",&kk); int n=strlen(a+1); ll Right=chufaqumo((kuaisumi(2,n*kk)-1),(kuaisumi(2,n)-1)); ll Left=0; for(int i=1;i<=n;i++) { if(a[i]=='5'||a[i]=='0') Left+=kuaisumi(2,i-1); Left%=mod; } ll ans=kuaisucheng(Left,Right); printf("%I64d\n",ans); }}
D.一道煞笔贪心Bfs T T.我是萌萌哒D题题解
E.状压Dp+Lowbit优化.我是萌萌哒E题题解
- Codeforces Round #191 (Div. 2)
- Codeforces Round #191 (Div. 2).
- Codeforces Round #191 (Div. 2)
- Codeforces Round #191 (Div. 2) ABCD
- Codeforces Round #191 (Div. 2) <全>
- Codeforces Round #191 (Div. 2) 解题报告
- Codeforces Round #191 (Div. 2) D
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- Codeforces Round #108 (Div. 2)
- Codeforces Round #110 (Div. 2)
- Codeforces Round #122 (Div. 2)
- Codeforces Round #121 (Div. 2)
- eclipse项目中从外部导入的js文件报错如何解决
- opencv: 安装 & 可能遇到的问题 & 解决方案
- 文本分类的python实现-基于Xgboost算法
- hibernate的Configuration,SessionFactory,Session详解---【小白系列】0基础到熟练应用hibernate框架(二)
- Null条件操作符(?.)和Null合并操作符(??)会引起Unity 报 MissingReferenceException
- Codeforces Round #191 (Div. 2)
- 数字逆序
- 二分查找
- POJ3680_Intervals_离散化&&最小费用流
- 字符串转数组
- 文章标题
- 移动端开发过程中需要注意的问题
- Tensorflow实现卷积神经网络
- 使用Jquery仿模态对话框插件Remodal实现弹出响应窗口