Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂
1 second
256 megabytes
standard input
standard output
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).
Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.
Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
Print a single number — the minimum number of operations Simon needs to carry out his idea.
3 3
0 0 0
0 1 0
0 0 0
4
4 3
0 0 0
0 0 1
1 0 0
0 0 0
2
In the first sample, the sequence of operations can be like this:
- For the first time you need to choose cell (2, 2) and corner (1, 1).
- For the second time you need to choose cell (2, 2) and corner (3, 3).
- For the third time you need to choose cell (2, 2) and corner (3, 1).
- For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
- For the first time you need to choose cell (3, 1) and corner (4, 3).
- For the second time you need to choose cell (2, 3) and corner (1, 1).
题意:n*m的矩阵涂色 每次选取两个点 1个顶点 1个标记为1的点 形成矩形并涂色 问最少要涂几次使得n*m的矩阵全部涂满
题解: 贪心 特判顶点,边界
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
int n,m;
int mp[][];
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
}
}
if(mp[][m]==||mp[n][m]==||mp[][]==||mp[n][]==)
{
printf("1\n");
return ;
}
for(int i=;i<=n-;i++){
if(mp[i][]==)
{
printf("2\n");
return ;
}
}
for(int i=;i<=n-;i++){
if(mp[i][m]==)
{
printf("2\n");
return ;
}
}
for(int i=;i<=m-;i++){
if(mp[][i]==)
{
printf("2\n");
return ;
}
}
for(int i=;i<=m-;i++){
if(mp[n][i]==)
{
printf("2\n");
return ;
}
}
printf("4\n");
return ;
}
1 second
256 megabytes
standard input
standard output
A permutation p is an ordered group of numbers p1, p2, ..., pn, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p1, p2, ..., pn.
Simon has a positive integer n and a non-negative integer k, such that 2k ≤ n. Help him find permutation a of length 2n, such that it meets this equation: .
The first line contains two integers n and k (1 ≤ n ≤ 50000, 0 ≤ 2k ≤ n).
Print 2n integers a1, a2, ..., a2n — the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.
1 0
1 2
2 1
3 2 1 4
4 0
2 7 4 6 1 3 5 8
Record |x| represents the absolute value of number x.
In the first sample |1 - 2| - |1 - 2| = 0.
In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.
In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0.
题意:构造a数列 使得满足上述的式子
题解:1~2n排列如下
(2n 2n-1) (2n-2 2n-3) ...... (4 3)(2 1)
=> 1 ,1 .....1,1
根据k 的大小 反转k对 输出即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll n,k;
struct node
{
ll l,r;
ll w;
}N[];
int main()
{
scanf("%I64d %I64d",&n,&k);
ll exm=n*;
for(int i=;i<=n;i++)
{
N[i].l=exm--;
N[i].r=exm--;
N[i].w=;
}
for(int i=;i<=n;i++)
{
if(k==)
break;
if(k>=N[i].w)
{
k-=N[i].w;
swap(N[i].l,N[i].r);
}
}
for(int i=;i<=n;i++)
printf("%I64d %I64d ",N[i].l,N[i].r);
return ;
}
1 second
256 megabytes
standard input
standard output
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.
Simon loves fractions very much. Today he wrote out number on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction:
, where number t equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction.
Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).
The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109) — the size of the array and the prime number.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
2 2
2 2
8
3 3
1 2 3
27
2 2
29 29
73741817
4 5
0 0 0 0
1
In the first sample . Thus, the answer to the problem is 8.
In the second sample, . The answer to the problem is 27, as 351 = 13·27, 729 = 27·27.
In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.
In the fourth sample . Thus, the answer to the problem is 1.
题意: 通分之后 求分子与分母的gcd 对1e9+7取模
题解:找到分子各项中最小的指数 并标记指数存在的次数 从低指数向上不断进位
注意所求指数应当小于等于分母的指数 之后用到快速幂。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll n,x;
ll a[];
ll b[];
map<ll,ll>mp;
ll sum;
ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
ll quickmod(ll aa,ll bb)
{
ll re=;
while(bb)
{
if(bb&)
re=(re*aa)%mod;
aa=(aa*aa)%mod;
bb=bb>>;
}
return re%mod;
}
int main()
{
sum=;
int jishu=;
mp.clear();
scanf("%I64d %I64d",&n,&x);
for(ll i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
for(ll i=;i<=n;i++)
{
ll exm=sum-a[i];
if(mp[exm]==)
{
b[jishu++]=exm;
}
mp[exm]++;
}
sort(b,b+jishu);
ll ans=b[];
while()
{
if(mp[ans]%x==){
mp[ans+]+=(mp[ans]/x);
ans++;
}
else
break;
}
if(ans>sum)
ans=sum;
printf("%I64d\n",(quickmod(x,ans)%mod));
return ;
}
Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂的更多相关文章
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round #303 (Div. 2) D 贪心
D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #545 (Div. 2) D 贪心 + kmp
https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...
- Codeforces Round #547 (Div. 3) G 贪心
https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...
随机推荐
- CSP201604-2:俄罗斯方块
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- Java实现在线预览功能
java实现在线预览功能,需要用到 jacob.dll jacob.jar 预览pdf所需js pdfobject.min.js 将上传文件转为pdf保存. <div class=&qu ...
- 每日Scrum--No.3
Yesterday:帮着队友一起打开地图 Today:学习迪杰斯特拉算法,试着编写程序代码 Problem:语法逻辑出错,在执行的时候,有的时候出现死循环,有的时候屏幕出现null和乱码.语句的编写有 ...
- 软工实践-Alpha 冲刺 (7/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成非功能的主界面制作 ...
- HDU 5391Z ball in Tina Town 数论
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5391 bc: http://bestcoder.hdu.edu.cn/contests/c ...
- Alpha 冲刺5
队名:日不落战队 安琪(队长) 今天完成的任务 组织第五次站立式会议(半冲刺总结交流会). 完成草稿箱前端界面. 明天的计划 回收站前端界面. 尝试去调用数据. 还剩下的任务 信息修改前端界面. 遇到 ...
- 第一章 JavaScript简介
DOM级别 DOM1:映射文档的结构 DOM2: DOM视图,定义了跟踪不同文档视图的接口(例如CSS应用前后的文档) DOM事件,定义了事件和事件处理的接口 DOM样式,定义了基于CSS为元素应用样 ...
- SSL 重点SSL会话步骤
SSL.TLS协议 在wiki百科查看下,两者的区别 实现SSL协议的软件 OpenSSL开源软件 SSL会话步骤 1:客户端向服务端索取CA证书,然后验证证书 2:客户端与服务端约定一个通信中使 ...
- 第八次JAVA语言笔记
- [Google] 看雪论坛: 安卓碎片化的情况
2018年10月28日早间消息,谷歌方面发布了Android各版本的最新份额数据,截止到10月26日.即便是已经推出3个月了,Android 9 Pie系统的用户数仍旧没有超过0.1%,导致未出现在榜 ...