HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))
Double Dealing
Time Limit: 50000/20000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1924 Accepted Submission(s): 679
k players in the usual way: the top card to player 1, the next to player 2, the
kth to player k, the k+1st to player 1, and so on. Then pick up the cards – place player 1′s cards on top, then player 2, and so on, so that player
k’s cards are on the bottom. Each player’s cards are in reverse order – the last card that they were dealt is on the top, and the first on the bottom.
How many times, including the first, must this process be repeated before the deck is back in its original order?
n and k (1≤n≤800, 1≤k≤800). The input will end with a line with two 0s.
All possible inputs yield answers which will fit in a signed 64-bit integer.
1 3
10 3
52 4
0 0
1
4
13
pid=4258" target="_blank">4258
pid=4260" target="_blank">4260
pid=4261" target="_blank">4261
4262求置换群循环节的lcm
注意lcm(x1..xn)=lcm(x1,lcm(x2..xn)!=x1*..*xn/gcd
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
char s[]="no solution\n"; class Math
{
public:
ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);}
ll abs(ll x){if (x>=0) return x;return -x;}
ll exgcd(ll a,ll b,ll &x, ll &y)
{
if (!b) {x=1,y=0;return a;}
ll g=exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
return g;
}
ll pow2(ll a,int b,ll p)
{
if (b==0) return 1;
if (b==1) return a;
ll c=pow2(a,b/2,p);
c=c*c%p;
if (b&1) c=c*a%p;
return c;
}
ll Modp(ll a,ll b,ll p)
{
ll x,y;
ll g=exgcd(a,p,x,y),d;
if (b%g) {return -1;}
d=b/g;x*=d,y*=d;
x=(x+abs(x)/p*p+p)%p;
return x;
}
int h[MAXN];
ll hnum[MAXN];
int hash(ll x)
{
int i=x%MAXN;
while (h[i]&&hnum[i]!=x) i=(i+1)%MAXN;
hnum[i]=x;
return i;
}
ll babystep(ll a,ll b,int p)
{
MEM(h) MEM(hnum)
int m=sqrt(p);while (m*m<p) m++;
ll res=b,ans=-1; ll uni=pow2(a,m,p);
if (!uni) if (!b) ans=1;else ans=-1; //特判
else
{ Rep(i,m+1)
{
int t=hash(res);
h[t]=i+1;
res=(res*a)%p;
}
res=uni; For(i,m+1)
{
int t=hash(res);
if (h[t]) {ans=i*m-(h[t]-1);break;}else hnum[t]=0;
res=res*uni%p;
} }
return ans;
}
}S; int a[10000+10];
bool b[10000+10];
int p[10000+10];
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout); int n,k;
while(cin>>n>>k)
{
if (n+k==0) return 0;
int s=0;
For(j,k)
for(int i=n/k*k+j>n?n/k*k+j-k:n/k*k+j;i>=1;i-=k) a[++s]=i; // For(i,n) cout<<a[i]<<' '; int tot=0; MEM(b)
For(i,n)
{
if (!b[i])
{
int t=i; b[i]=1;
int len=1;
do {
b[t]=1;
t=a[t]; ++len;
// cout<<t<<endl; } while (!b[t]);
len--; p[++tot]=len;
}
} sort(p+1,p+1+tot);
tot=unique(p+1,p+1+tot)-(p+1); // For(i,tot) cout<<p[i]<<' '; ll ans=1;
For(i,tot) ans=ans/S.gcd(p[i],ans)*p[i]; cout<<ans<<endl; } return 0;
}
HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))的更多相关文章
- hdu 4259 Double Dealing
思路: 找每一个数的循环节,注意优化!! 每次找一个数的循环节时,记录其路径,下次对应的数就不用再找了…… 代码如下: #include<iostream> #include<cst ...
- HDU 4259 - Double Dealing(求循环节)
首先将扑克牌进行一次置换,然后分解出所有的循环节,所有循环节的扑克牌个数的最小公倍数即为答案 #include <stdio.h> #include <string.h> #i ...
- HDOJ 4259 Double Dealing
找每一位的循环节.求lcm Double Dealing Time Limit: 50000/20000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 4529 Double Dealing (置换群)
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...
- 有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M。 需要选出若干个x,使这几个x的和与 M 最接近。 请描述实现算法,并指出算法复杂度
题目:有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M. 需要选出若干个x,使这几个x的和与 M 最接近. 请描述实现算法,并指出算法复杂度. 代码如下: #in ...
- HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu 1908 Double Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...
- POJ-2429 GCD & LCM Inverse---给出gcd和lcm求原来两个数
题目链接: https://cn.vjudge.net/problem/POJ-2429 题目大意: 给出两个数的gcd和lcm,求原来的这两个数(限定两数之和最小). 解题思路: 首先,知道gcd和 ...
- HDU 1568 double 快速幂
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- Intellij IDEA14配置
一.下载 官网下载地址:http://www.jetbrains.com/idea/ 目前最新的版本是15,发现15注册比较麻烦,好像需要只能通过联网激活.而网上14的离线注册码一大堆,就下载了14, ...
- 【Spring】IOC
浅谈IOC IOC的理论背景 图1:传统系统中,对象之间相互引用的一幅图,在采用面向对象方法设计的软件系统中,它的底层的实现都是由n个对象所组成的,所有的对象通彼此之间的合作最终实现系统的业务逻辑,如 ...
- [ USACO 2018 OPEN ] Out of Sorts (Gold)
\(\\\) \(Description\) 运行以下代码对一长为\(N\)的数列\(A\)排序,不保证数列元素互异: cnt = 0 sorted = false while (not sorted ...
- java 向上,向下转型
在对Java学习的过程中,对于转型这种操作比较迷茫,特总结出了此文.例子参考了<Java编程思想>. 目录 几个同义词 向上转型与向下转型 例一:向上转型,调用指定的父类方法 例二:向上转 ...
- Python 之字符串操作
# capitalize()将字符串的第一个字符转换为大写 # center(width, fillchar)返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格. ...
- C#当中的out关键字(借鉴于CSDN)
一丶与ref关键字一样,out关键字也是按引用来传递的.out 关键字会导致参数通过引用来传递.这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化.若要使用 out 参数 ...
- Vue中.sync修饰符
Vue 中 sync的作用 <FatherComponent :a.sync = 'b'><FatherComponent /> 子组件中emit('update:a',... ...
- discourse论坛迁移
在源设备的操作备份数据文件tar -czvf discoursefile716.tar.gz /var/discourse然后把此discoursefile716.tar.gz文件传到需要迁移的设备上 ...
- PHP面试:说下什么是堆和堆排序?
堆是什么? 堆是基于树抽象数据类型的一种特殊的数据结构,用于许多算法和数据结构中.一个常见的例子就是优先队列,还有排序算法之一的堆排序.这篇文章我们将讨论堆的属性.不同类型的堆以及堆的常见操作.另外我 ...
- Flask - 模板语言jinja2 和render_template高级用法
目录 Flask - 模板语言jinja2 和render_template高级用法 一. 字典传递至前端 二. 列表传入前端Jinja2 模板的操作: 三. 大字典传入前端 Jinja2 模板 四. ...