hdu 5392 Infoplane in Tina Town(数学)
Tina and Town were playing a game on this stone. First, a permutation of numbers from 1 to n were displayed on the stone. Town exchanged some numbers randomly and Town recorded this process by macros. Town asked Tine,”Do you know how many times it need to turn these numbers into the original permutation by executing this macro? Tina didn’t know the answer so she asked you to find out the answer for her.
Since the answer may be very large, you only need to output the answer modulo 3∗230+1=3221225473 (a prime).
For each test case, the first line is an integer n representing the length of permutation. n≤3∗106
The second line contains n integers representing a permutation A1...An. It is guaranteed that numbers are different each other and all Ai satisfies ( 1≤Ai≤n ).
3
1 3 2
6
2 3 4 5 6 1
6
中文题目:
Tina Town 的镇中有一块表面平整光滑的大石头, 当人面向它走进时, 石头的表面就会亮起, 显示出它的用途. 这块石头是前人留下来的东西,是Tina Town算力和控制系统的核心, 同时它也可以显示Tina Town的大小事和行人关注的内容,也可以作为公共的计算机使用 ,极大地方便了人们的生活(特别是上街忘带终端的人们啦). Tina和Town在这块大石头上玩一个游戏. 石头上首先显示出了1到n排列的n个数,Town则随机交换了一些数, 然后Town通过宏将这个交换的过程记录下来了. Town 问 Tina: 你知道执行几次这个宏, 这些数会恢复最早的排列嘛? Tina是一个蠢蠢的女孩子, 当然不知道啦, 于是她想向你请教呢.
来自官方题解:
给出一个置换,求它的循环长度。数据范围 3\times 10^63×106。
其实没有什么好说的,就分解成循环求长度的最小公倍数就好了。对于这个模数要用unsigned int存,这个最小公倍数的求法不能用欧几里得,直接每次分解质因数,用线性筛预处理一下就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<stdlib.h>
using namespace std;
#define ll long long
#define N 3000006
#define MOD 3221225473
int n;
int a[N];
int vis[N];
int cou[N];
inline int sca() //输入外挂
{
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
a[i]=sca();
vis[i]=;
cou[i]=;
} for(int i=;i<=n;i++)
{
if(vis[i]) continue;
int len=;
int k=a[i];//k用来查找长度
while(k!=i)//如果当前的数与原来的位置不一样,则长度加1
{
len++;
vis[k]=;
k=a[k];
}
for(int j=;j*j<=len;j++)//这边用来计算最小公倍数,不能用lcm,因为这里是单个数
{
int c=;
if(len%j==)//如果可以分解质因数的话,则连续分解
{
while(len%j==)
{
c++;
len=len/j;
}
cou[j]=max(cou[j],c);//记录这个质因数的个数
}
}
if(len>) cou[len]=max(cou[len],);//记住这里要加上,比如说10来分解质因数,最后剩下5(10/2=5),则cou【5】也要算进去
} ll ans=;
for(int i=;i<=n;i++)//这边计算最小公倍数
{
for(int j=;j<cou[i];j++)
ans=ans*i%MOD;
}
printf("%I64d\n",ans);
}
return ;
}
3*2^{30}+1=32212254733∗230+1=3221225473(一个素数)的模就行了.
hdu 5392 Infoplane in Tina Town(数学)的更多相关文章
- HDU 5392 Infoplane in Tina Town
Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
- hdoj 5392 Infoplane in Tina Town
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5392 #include<stdio.h> #include<cstring> ...
- hdu5392 Infoplane in Tina Town(LCM)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Infoplane in Tina Town Time Limit: 14000/ ...
- hdu 5391 Zball in Tina Town 威尔逊定理 数学
Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- HDU 5391Z ball in Tina Town 数论
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5391 bc: http://bestcoder.hdu.edu.cn/contests/c ...
- hdu 5391 Zball in Tina Town(打表找规律)
问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候, ...
- HDU 5391 Zball in Tina Town【威尔逊定理】
<题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...
- HDU.5394.Trie in Tina Town(回文树)
题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...
- HDU 5391 Zball in Tina Town (打表,水)
题意: Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大1倍.在第二天的时候,它会变大2倍.在第n天的时候,它会变大n倍.zball原来的体积是1.Ti ...
随机推荐
- Bestcoder #47 B Senior's Gun
Senior's Gun Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Spark的日志配置
在測试spark计算时.将作业提交到yarn(模式–master yarn-cluster)上,想查看print到控制台这是imposible的.由于作业是提交到yarn的集群上,so 去yarn集群 ...
- [ES6] Module export
Default export: Default export is easy way to export a function to outside module. //flash-message.j ...
- jquery获取checkbox被选中的值
只用一个循环,就可以找出被选中的checkbox的值 var s; $("[name = b]:checkbox").each(function () { ...
- php模块memcache和memcached区别分析
zm总结:尽量使用memcached就好了 1.目前大多数php环境里使用的都是不带d的memcache版本,这个版本出的比较早,是一个原生版本,完全在php框架内开发的.与之对应的带d的memcac ...
- 读书笔记_Effective_C++_条款十五:在资源类管理类中提供对原始资源的访问
void f(int* a) { cout <<* a << endl; } int main() { shared_ptr<int> p(new int(3)); ...
- Python 基础教程中的问题及解决方案(1)
1. 在ubuntu中,调用终端时如: f = open('/home/theone/test_input.txt', 'r') 中的txt格式文本不能加后缀 正确的应为: f = open('/h ...
- AES CBC 128的实现
原由 AES已经变成目前对称加密中最流行算法之一,AES可以使用128.192.和256位密钥,并且用128位分组加密和解密数据. 项目中需要使用AES对密码信息进行加密,由嵌入式设备使用C语言进行加 ...
- matlab读取多幅图片,并对读取的图片降采样和双三次插值
clear all clc im = {}; %%创建字典im以保存读取的图片 dis = dir('C:\Users\KCl\Documents\MATLAB\SRCNN\Set5\*.bmp'); ...
- 开始3D编程前需注意的十件事
http://www.csdn.net/article/2013-06-21/2815949-3d-programming 原文作者Vasily Tserekh是名3D编程爱好者,他发表了一篇博文&l ...