CSU 1617: Itself is Itself(强连通缩点)思想转换到图论

1617: Itself is Itself

Time Limit: 6 Sec  Memory Limit: 128 MB

Submit: 21  Solved: 4

[Submit][Status][Web
Board
]

Description

Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.

Zuosige writes an integer n and a polynomial function:

P(x) = (a0+a1*x+a2*x2+…+am*xm)
mod n.

He wants to know how many subsets of set {0, 1, 2, … , n-2, n-1} satisfies following property: the range of P(x) from the subset is itself. Pay attention to that empty set is also a valid subset.

Input

The first line contains one integer T, indicating the number of test cases.

In one test case, there are two lines.

In the first line, there are two integers n and m (1<=n<=10000, 1<=m<=1000).

In the second line, there m+1 integers. The i-th integer indicating ai-1 (0<=ai<=10000).

Output

For each test case, output an integer in a line indicating the answer. The answer can be very large, so you need just output the answer mod 1e9+7.

Sample Input

2
3 1
0 1
3 1
1 1

Sample Output

8
2

HINT

Source

#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
#define modd 1000000007
const int N= 10005;

int n,c[N];
int dfn[N],low[N],Stack[N],flag[N],vist[N],num[N],top,deep,tn;
vector<int>mapt1[N];

void init()
{
    for(int i=0;i<=n;i++)
    {
        mapt1[i].clear();
        dfn[i]=0;
        num[i]=0;
        vist[i]=0;
    }
    tn=top=deep=0;
}
int tt;
void tarjan(int u)
{
    vist[u]=tt;
    deep++;
    dfn[u]=low[u]=deep;
    Stack[++top]=u;

    int len=mapt1[u].size();
    for(int i=0;i<len;i++)
    {
        int v=mapt1[u][i];

        if(vist[v]==0)
        {
            tarjan(v);
            if(low[u]>low[v])
                low[u]=low[v];
        }
        else if(vist[v]==tt&&low[u]>dfn[v])//注意vist[v]==tt
            low[u]=dfn[v];
    }
    if(low[u]==dfn[u])
    {
        tn++;
        while(u!=Stack[top])
        {
            flag[Stack[top]]=tn; num[tn]++;top--;
        }
        flag[Stack[top]]=tn; num[tn]++; top--;
    }
}
int out[N];
int rebuilMap()//用强连通缩点,
{
    tt=0;
    for(int i=0;i<n;i++)
        if(vist[i]==0)
        {
            tt++;
            tarjan(i);
        }

    memset(out,0,sizeof(out));
//缩点后的图无 有向环
    for(int i=0;i<n;i++)
    {
        int u=flag[i];
        for(int j=0;j<mapt1[i].size();j++)
        {
            int v=flag[mapt1[i][j]];
            if(u==v)
                continue;
            out[u]++;
        }
    }
    int number=0;
    for(int i=1;i<=tn;i++)
        if(out[i]==0)
            number++;
    return number;
}
int main()
{
    int T,m;

    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<=m;i++){
            scanf("%d",&c[i]);
            c[i]%=n;
        }

        init();
        for(int i=0;i<n;i++)
        {
            int ansa=c[0],x=i;
            for(int j=1;j<=m;j++){
                    ansa=(ansa+c[j]*x)%n;
                    x=(x*i)%n;
            }
            if(i!=ansa)
            mapt1[i].push_back(ansa);
        }

        int number=rebuilMap();
        long long ans = 1;
        for(int i=1;i<=number;i++)
            ans=(ans*2)%1000000007;
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-24 12:45:02

CSU 1617: Itself is Itself(强连通缩点)思想转换到图论的相关文章

Light OJ 1168 Wishing Snake 强连通缩点+哈密顿通路

题目来源:Light OJ 1168 Wishing Snake 题意:有点难看懂题意 看了一个小时再加别人的代码才懂意思 从0开始 输入的那些每一对u v 都要经过 就是从0到到达那些点 思路:首先缩点 每一个强连通分量里面的点都是可达的 缩点后的图是有向无环图 如果从0这个强连通分量可以出去到2个强连通分量 那么这两个强连通分量是不可能相互可达 所以可行的方案就是所有的强连通分量连成一线 一个一个串起来 除了第一个 出度是1入度是0和最后一个出度是0入度是1 其他都是入度等于出度是1 特判只

强连通缩点— HDU1827

强连通缩点以后最终形成的是一棵树 我们可以根据树的性质来看缩点以后的强连通分量图,就很好理解了 /* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring> #include<

BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点

题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问到所有的点. 代码: #include<iostream> #include<cstdio> #include<vector> #include<stack> #include<algorithm> #include<cstring> u

POJ3352Road Construction(边的双连通+强连通缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8673   Accepted: 4330 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

hdu3861The King’s Problem (强连通 缩点+最小路径覆盖)

The King's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1606 Accepted Submission(s): 584 Problem Description In the Kingdom of Silence, the king has a new problem. There are N cities in

UVA - 11324 The Largest Clique 强连通缩点+记忆化dp

题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每个点的权值就是当前强连通分量点的个数. /* Tarjan算法求有向图的强连通分量set记录了强连通分量 Col记录了强连通分量的个数. */ #include <iostream> #include<cstring> #include<cstdio> #include<string> #include<algo

HDU 4971 A simple brute force problem. 强连通缩点+最大权闭合图

题意: 给定n个项目,m个技术难题 下面一行n个数字表示每个项目的收益 下面一行m个数字表示攻克每个技术难题的花费 下面n行第i行表示 第一个数字u表示完成 i 项目需要解决几个技术难题,后面u个数字表示需要解决的问题标号. 下面m*m的矩阵 (i,j) = 1 表示要解决j问题必须先解决i问题. (若几个问题成环,则需要一起解决) 问:最大收益. 思路: 先给问题缩点一下,每个缩点后的点权就是这个点内所有点权和. 然后跑一个最大权闭合图. #include<stdio.h> #include

hdu 4635(强连通+缩点)

http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1381    Accepted Submission(s): 587 Problem Description Give a simple directed

poj2553 强连通缩点

The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10114   Accepted: 4184 Description We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called ve