UVA10294 Arif in Dhaka (First Love Part 2) —— 置换、poyla定理

题目链接:https://vjudge.net/problem/UVA-10294

 

 

题解:

白书P146~147。

 

为什么旋转i个间距,就有gcd(i,n)个循环,且每个循环有n/gcd(i,n)个元素?

证明:

   (gcd:最大公约数,lcm:最小公倍数)

      将珠子从0到n-1标号,对于旋转i位的置换,在以0号为起点,长度为t的一个循环节中,元素标号为:0,i%n,(i*2)%n,…,(i*(t-1))%n

      易知:(i*t)%n==0(循环大小为t,跳t次就回到初始点0),即 n*k == i*t,其中n,k,i,t为正整数,因此等式左右的最小值为lcm(n,i),即i*t==lcm(n,i),为什么i*t取最小值,即t取最小值?因为是从0第一次跳到0就完成整个循环的遍历,这个“第一次”就决定了是最早满足条件的那个t,即最小t。

      ∴ t == lcm(n,i)/i == ( n*i/gcd(n,i) )/i == n/gcd(n,i)

      ∴ 循环节t==n/gcd(n,i),循环节的个数为:n/t == gcd(n,i)

 

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 55;
18 
19 LL gcd(LL a, LL b)
20 {
21     return b==0?a:gcd(b,a%b);
22 }
23 
24 LL Pow[MAXN];
25 int main()
26 {
27     int n, t;
28     while(scanf("%d%d", &n,&t)!=EOF)
29     {
30         Pow[0] = 1;
31         for(int i = 1; i<=n; i++) Pow[i] = 1LL*Pow[i-1]*t;
32         LL a = 0, b = 0;
33         for(int i = 0; i<n; i++)
34             a += Pow[gcd(i,n)];
35         if(n%2)
36             b = 1LL*n*Pow[n/2+1];
37         else
38             b = 1LL*n/2*(Pow[n/2]+Pow[n/2+1]);
39 
40         printf("%lld %lld\n", a/n, (a+b)/2/n);
41     }
42 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8526548.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值