Sample Input
2
3
1 3 2
6
2 3 4 5 6 1
 
Sample Output
2
6

题意:给一个转置求它的循环长度

题解:分解成循环求最小公倍数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MOD 3221225473
#define N 100005
#define MIN 0
#define MAX 1000001 const int maxn = 3000005;
int a[maxn],vis[maxn],sum[maxn]; void fin(int num)
{
int tt;
for(int i = 2; i <= num; i++)
{
tt = 0;
while(num%i == 0)
{
num/=i;
tt++;
}
if(tt > sum[i])
sum[i] = tt;
}
} ll pow_mod(ll q,int n,ull mod)
{
if(n == 0)
return 1;
ll x = pow_mod(q,n/2,mod);
ll ans = (ll)x*x%mod;
if(n %2 == 1)
ans = ans *q % mod;
return ans;
} int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ll ans;
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
memset(vis,0,sizeof(vis));
memset(sum,0,sizeof(sum));
for(int i = 1; i <= n; i++)
{
if(vis[i])
continue;
int tmp = i;
int num = 0;
while(!vis[tmp])
{
vis[tmp] = 1;
tmp = a[tmp];
num ++;
}
//printf("num:%d\n",num);
fin(num);
} ans = 1;
for(int i = 2; i <= n; i++)
if(sum[i])
{
//printf("%d\n",sum[i]);
ans = (ans * pow_mod(i,sum[i],(ll)MOD))%MOD;
}
printf("%I64d\n",ans);
}
return 0;
}

  

hdu 5392的更多相关文章

  1. hdu 5392 Infoplane in Tina Town(数学)

    Problem Description There is a big stone with smooth surface in Tina Town. When people go towards it ...

  2. HDU 5392 BC #51

    就是求最大公倍数,但要用分解质因子求. 自己写的WA到爆.... #include<iostream> #include<stdio.h> #include<math.h ...

  3. HDU 5392 Infoplane in Tina Town

    Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 524288/524288 K (Jav ...

  4. hdoj 5392 Infoplane in Tina Town

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5392 #include<stdio.h> #include<cstring> ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. 关于5303狄惟佳同学的myod程序设计的补充实现

    关于5303狄惟佳同学的myod程序设计的补充实现 原版代码实现的局限 原版代码主函数 int main(int argc,char *argv[]) { if(strcmp(argv[1], &qu ...

  2. Python 单向循环链表

    操作 is_empty() 判断链表是否为空 length() 返回链表的长度 travel() 遍历 add(item) 在头部添加一个节点 append(item) 在尾部添加一个节点 inser ...

  3. 原始的Ajax方法 (异步的 JavaScript 和 XML -- (Extensible Markup Language 可扩展标记语言))

    <script language="javascript" type="text/javascript"> var request = false; ...

  4. JAVA_SE基础——12.运算符的优先级

    优先级 操作符 含义 关联性 用法 ---------------------------------------------------------------- 1 [ ] 数组下标 左 arra ...

  5. navicate连接不上阿里云mysql

    一 用xshell连接进入服务器: 二 使用命令连接mysql mysql -uroot -p 三 更新权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDE ...

  6. Spring Cache扩展:注解失效时间+主动刷新缓存(二)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. ASP.NET Web API编程——模型验证与绑定

    1.模型验证 使用特性约束模型属性 可以使用System.ComponentModel.DataAnnotations提供的特性来限制模型. 例如,Required特性表示字段值不能为空,Range特 ...

  8. gradle入门(1-1)gradle的概念和使用

    一.Gradle是什么 Gradle是一种Java应用构建工具,它采用领域特定语言 Groovy 语法实现配置. 1.Gradle的基本概念 项目:项目的配置 即 build.gradle. 任务:任 ...

  9. java线程池01-ThreadPoolExecutor构造方法参数的使用规则

    为了更好的使用多线程,JDK提供了线程池供开发人员使用,目的在于减少线程的创建和销毁次数,以此达到线程的重复利用. 其中ThreadPoolExecutor是线程池中最核心的一个类,我们先简单看一下这 ...

  10. Django中ORM介绍和字段及其参数

    ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述 ...