HDU5399——贪心——Too Simple

http://acm.hdu.edu.cn/showproblem.php?pid=5399

/*
先特判有重复数的情况,如果有就输出-1
如果全是数字那么就判是否符合条件
剩下的就是m!的(cout-1)次
*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-18 18:40:18
* File Name     :1004.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int b[MAXN][MAXN];
int a[MAXN][MAXN];
int c[MAXN];
ll fact(int x)
{
    ll ans = 1;
    for(int i = 1; i <= x; i++)
        ans = ans * i % MOD;
    return ans;
}
int main(){
    int n, m;
    while(~scanf("%d%d", &m, &n)){
        int cout = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d", &a[i][1]);
            if(a[i][1] == -1){cout++; continue;}
            for(int j = 2; j <= m; j++)
                scanf("%d", &a[i][j]);
        }
        int flag = 0;
        for(int i = 1; i <= n; i++){
            memset(c, 0, sizeof(c));
                if(a[i][1] == -1) continue;
                for(int j = 1; j <= m; j++){
                  if(!c[a[i][j]]) c[a[i][j]] = 1;
                    else flag = 1;
                }
        }
       if(flag == 1){
          puts("0");
          continue;
       }
       int p;
       flag = 0;
       if(cout == 0){
           for(int i = 1; i <= m; i++){
               p = i;
               for(int j = n; j >= 1; j--)
                   p = a[j][p];
               if(p!=i) flag = 1;
           }

           if(flag == 1){
               puts("0");
               continue;
           }
           //    for(int i = 1; i <= m; i++){
           //         printf("%d ", b[n][i]);}
           else {
               puts("1");
               continue;
           }
       }
         ll ans = 1;
         ll ret = fact(m);
        for(int i = 1; i < cout; i++){
            ans = ans * ret  % MOD;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

  

时间: 2024-08-02 11:00:37

HDU5399——贪心——Too Simple的相关文章

8VC Venture Cup 2016 - Elimination Round

在家补补题   模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, int &y, char ch) { if (ch == 'U') x--; if (ch == 'D') x++; if (ch == 'L') y--; if (ch == 'R') y++; } int main(void) { int n; scanf ("%d", &

HDU 4974 A simple water problem(贪心)

HDU 4974 A simple water problem 题目链接 签到题,很容易贪心得到答案是(sum + 1) / 2和ai最大值的最大值 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100005; typedef long long ll; int t, n; ll a, Max, sum; int main(

HDU 4976 A simple greedy problem. 贪心+DP

题意: 给定n<=1000个小兵,A每次都能使小兵掉1点血,B每次能使所有小兵掉1点血,A.B轮流攻击,每次轮到A他会选择是否攻击,轮到B必须攻击.求A最多能杀死多少小兵.(当小兵血量为1时被攻击到视为被杀死) 思路: 如果所有小兵血量都不一样,A必定能杀死所有小兵.如果有小兵血量相同,那么A必定会攻击一些小兵使他们血量不同.所以我们可以把A的攻击分为两类:用来刚好杀死小兵的一次, 和用来使小兵血量不同的攻击. 我们可以用贪心的思想预处理出怎样用最小的攻击次数让小兵的血量都不同. 然后我们可以用

Codeforces Round #316 (Div. 2) B Simple Game 贪心

贪心,如果m分成的两个区间长度不相等,那么选长的那个区间最接近m的位置,否则选m-1位置,特判一下n等于1的情况 #include<bits/stdc++.h> using namespace std; int main() { int n,m; scanf("%d%d",&n,&m); if(n == 1){ printf("1"); return 0; } int d1 = m-1, d2 = n-m; if(d1<d2){ p

hdu5399(2015多校9)--Too Simple

题目链接:点击打开链接 题目大意:有m个映射,从1到n映射到1到n,记为f1,f2,f3,,,fm,并且这些映射满足f1( f2( f3(,,,,fm(i) ) ) ) = i现在已知几个映射的值,还有几个映射是不知道的,问不知道的映射一共有几种可能的组合方式. 输入n m,之后m行,如果一行的第一个数为-1,代表这一个映射fi是不知道的,否则一行有n个数,第i行的第j个数字x代表fi(j) = x 问最终的种类(对1e9+7取余) 1.如果映射不是单射,也就是存在fi(x) = fi(y)并且

[hdu5399 Too Simple]YY

题意:m个{1,2...n}→{1,2...,n}的函数,有些已知有些未知,求对任意i∈{1,2,...,n},f1(f2(...(fm(i)))=i的方案总数,为了方便简记为F(i) 思路:如果存在一个f,当i!=j时,有f(i)=f(j),那么方案数为0,因为由里到外进行f运算,两个不同的数到这里来了变成了i和j,然后变成了同一个数,最终还是等于同一个数,所以在最外面至少有一个不会满足F(x)=x.如果f全部确定了,那么只需对每个i计算一下F(i)即可确定答案.如果f没确定的个数为cnt,则

hdu - 4974 - A simple water problem(贪心 + 反证)

题意:N个队(N <= 100000),每个队有个总分ai(ai <= 1000000),每场比赛比赛双方最多各可获得1分,问最少经过了多少场比赛. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 -->>我们应该尽量使每场比赛的得分为1 : 1,这样可以达到最少的比赛场数(不小于单个队伍的分数). 假设有2场比赛的比分为1 : 0, 1)a : b = 1 : 0,c : d = 1 : 0,这时可以安排a : c = 1

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will

C语言贪心(2)___田忌赛马(Hdu 1052)

Problem Description Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others." "Both of Tian and the king have t