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个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...
随机推荐
- Spring学习(3):Spring概述(转载)
1. Spring是什么? Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发. 在面向对象思想中 ...
- ovs源码阅读--流表查询原理
背景 在ovs交换机中,报文的处理流程可以划分为一下三个步骤:协议解析,表项查找和动作执行,其中最耗时的步骤在于表项查找,往往一个流表中有数目巨大的表项,如何根据数据报文的信息快速的查找到对应的流表项 ...
- Composer指南
安装 windows中安装Composer 一般来说,windows下安装composer有两种办法,一种是直接下载并运行Composer-Setup.exe,这种方法在中国似乎很难完成安装.另一种就 ...
- Java fluent风格(转载)
转载:java Fluent风格 一.我们先写一个通常的,即不使用fluent风格 1.实体类 package com.xbq.demo.stu; /** * @ClassName: Student ...
- Visionpro学习网
重码网是一个在线机器视觉学习网站,推出了Halcon,Visionpro机器视觉学习视频教程,视频内容通俗易懂,没有编程基础的同学,照着视频练习,也同样可以学会. 学机器视觉,拿高薪,成就技术大拿.重 ...
- Scrum立会报告+燃尽图(十一月二十日总第二十八次):功能开发与纪录版本控制报告
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- 20172305 2018-2019-1 《Java软件结构与数据结构》第六周学习总结
20172305 2018-2019-1 <Java软件结构与数据结构>第六周学习总结 教材学习内容总结 本周内容主要为书第十章内容: 树(一种非线性结构,其中的元素被组织成一个层次结构) ...
- 《我是IT小小鸟》读后感
<我是IT小小鸟>读后感 说实话,我根本不喜欢看这本书,要不是因为老师要求我也不会去看的,其实当老师提起这本书的时候我还是有点兴趣,去看的,可是看了很多后,觉得这根本不适合我,里面说的都是 ...
- 关于在eclipse Oxygen 2017环境下spring3.2 asm的异常
今天在oxygen 2017下构建spring3.2+mybatis项目时,发生异常,大概是说无法解析我mapper.class文件,错误发生在core.asm中<unknow:source&g ...
- [2017BUAA软工]第一次博客作业
一.一些疑问 看书看得比较慢,暂时只思考了以下几个问题,有些自问自答,不知道符合不符合要求…… [1] 第一章中书上提到了这样一个例子: “如果一架民用飞机上有需求,用户使用它的概率是百万分之一,你还 ...