WustOJ 1575 Gingers and Mints(快速幂 + dfs )
1575: Gingers and Mints
Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lld
Submitted: 24 Accepted: 13
[Submit][Status][Web Board]
Description
owns a farmland, the farmland has n * m grids. Some of the grids are
stones, rich soil is the rest. fcbruce wanna plant gingers and mints on
his farmland, and each plant could occupy area as large as possible. If
two grids share the same edge, they can be connected to the same area.
fcbruce is an odd boy, he wanna plant gingers, which odd numbers of
areas are gingers, and the rest area, mints. Now he want to know the
number of the ways he could plant.
Input
Output
For each test case, print the answer mod 1000000007 in one line.
Sample Input 
2
3 3
YNY
YNN
NYY
3 3
YYY
YYY
YYY
Sample Output
4
1
HINT
For the
first test case, there are 3 areas for planting. We marked them as A, B
and C. fcbruce can plant gingers on A, B, C or ABC. So there are 4 ways
to plant gingers and mints.
Source
Author
fcbruce
#include<cstdio>
#include<cstring>
using namespace std; #define maxn 200
char pic[maxn][maxn];
int vis[maxn][maxn];
int dx[] = {-,,,};
int dy[] = {,,-,};
int n,m; void dfs(int x, int y)
{
for(int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(!vis[nx][ny] && pic[nx][ny] == 'Y' && nx >= && nx < n && ny >= && ny < m)
{
vis[nx][ny] = ;
dfs(nx,ny);
}
}
}
int pow_mod(int a,int n,int m)
{ if(n==)
return ;
int x=pow_mod(a,n/,m);
long long ans=(long long)x*x%m;
if(n%==)
ans=ans*a%m;
return (int )ans;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
memset(vis, , sizeof vis);
for(int i = ; i < n; i++)
scanf("%s", pic[i]);
int cnt = ;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
if(!vis[i][j] && pic[i][j] == 'Y')
{
vis[i][j] = ;
cnt++;
dfs(i,j);
}
}
printf("%d\n",pow_mod(,cnt-,));
}
return ;
}
WustOJ 1575 Gingers and Mints(快速幂 + dfs )的更多相关文章
- HDU.1575 Tr A ( 矩阵快速幂)
HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others) Me ...
- hdu 1575 Tr A(矩阵快速幂)
今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...
- hdu 1575 Tr A(矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...
- HDU 1575(裸矩阵快速幂)
emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][ ...
- hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)
Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...
- 简单矩阵快速幂(HDU Tr A 1575)
题目中所给的方阵就是一个矩阵,而就是只要将题目所给矩阵不断进行相乘即可,本题中我采用的是直接重载运算符*,使矩阵每一个都进行运算,可以简化为只对对角线上的元素进行运算.最后所得结果就只需将最终的矩阵上 ...
随机推荐
- split
import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; /** * 解析知网文章的页面 ...
- Gradle[1]gradle distZip时,增加目录信息到zip中
在使用gradle distZip打包生成应用程序时,发现只把src目录,及依赖的本地libs目录下的Jar包达到生成zip中, 如果项目还需要一些库文件,在和src同级目录,如sigar目录下,而且 ...
- solr全文检索基本原理
场景:小时候我们都使用过新华字典,妈妈叫你翻开第38页,找到“坑爹”所在的位置,此时你会怎么查呢?毫无疑问,你的眼睛会从38页的第一个字开始从头至尾地扫描,直到找到“坑爹”二字为止.这种搜索方法叫做顺 ...
- windows使用命令行杀进程
在windows有时使用任务管理器杀进程,一直杀不掉: 这个时候,可以使用命令行: 先使用tasklist 命令查看当前系统中的进程列表,然后针对你要杀的进程使用taskkill命令 如要杀nginx ...
- 从epoll构建muduo-11 单线程Reactor网络模型成型
mini-muduo版本传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ver ...
- 【winform程序】自定义webrowser控件调用IE的版本
修改注册表: bit: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROW ...
- Asp.net +Jquery-uploadify多文件上传
页面代码如下: <link href="JS/jquery.uploadify/uploadify.css" rel="stylesheet" type= ...
- next数组
首先看看next数组值的求解方法例如: 模式串 a b a a b c a c next值 0 1 1 2 2 3 1 2 next数组的求解方法是:第一位的next值为0 ...
- 3.1,pandas【基本功能】
一:改变索引 reindex方法对于Series直接索引,对于DataFrame既可以改变行索引,也可以改变列索引,还可以两个一起改变. 1)对于Series In [2]: seri = pd.Se ...
- 学习在创建好的工程里面添加CoreData
在学习CoreData中, 在建工程后, 没有添加, 于是就参考网络文章进行更改. 这几天在学习做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项 ...