P2504洗牌(扩欧)

洗牌
在这里插入图片描述
根据题目模拟了几次洗牌过程,得到了第一层逻辑, i f ( x < = ( n / 2 ) ) x 1 = 2 ∗ x     e l s e   i f ( x > ( n / 2 ) ) x 1 = ( x − ( n / 2 ) − 1 ) ∗ 2 + 1 ; − > x 1 = ( 2 ∗ x − n − 2 ) + 1 − > x 1 = ( 2 ∗ x − ( n + 1 ) ) 因 为 2 ∗ x − ( n + 1 ) < n 所 以 x 1 = 2 ∗ x ( m o d   n + 1 ) , 总 式 子 也 是 x 1 = 2 ∗ x ( m o d   n + 1 ) 题 目 变 为 2 m ∗ x = L ( m o d   n + 1 ) − > 2 m ∗ x + ( n + 1 ) ∗ Y = L if(x<=(n/2))x1=2*x\ \ \ else \ if(x>(n/2))x1=(x-(n/2)-1)*2+1;->x1=(2*x-n-2)+1->x1=(2*x-(n+1))因为2*x-(n+1)<n所以x1=2*x(mod\ n+1),总式子也是x1=2*x(mod \ n+1) 题目变为2 ^ m *x=L(mod\ n+1)->2^ m*x+(n+1)*Y=L if(x<=(n/2))x1=2x   else if(x>(n/2))x1=(x(n/2)1)2+1;>x1=(2xn2)+1>x1=(2x(n+1))2xn+1<nx1=2x(mod n+1),x1=2x(mod n+1)2mx=L(mod n+1)>2mx+(n+1)Y=L 扩欧取最小值即可

//package KMP;
import java.util.Scanner;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.LinkedList;
import java.util.Queue;
import java.math.BigInteger;
public class Main {
static long x,y;
public static long exd_gcd(long a,long b)
{
	if(b==0)
	{
		x=1;y=0;
		return a;
	}
	long d=exd_gcd(b,a%b);
	y^=x;
	x^=y;
	y^=x;
	y-=(a/b)*x;
	return d;
}
public static long res(long s,long mod)
{
	exd_gcd(s,mod);
	//System.out.println("X"+x+"Y"+y);
	return x>0?x%mod:(x%mod+mod)%mod;
}
public static long mul(long a,long b,long mod)
{
	long ans=0;
	while(b!=0)
	{
		if((b&1)==1)
			ans=(ans+a)%mod;
		a=(a+a)%mod;
		b>>=1;
	}
	return ans;
}
public static long quicks(long a,long b,long mod)
{
	long ans=1;
	while(b!=0)
	{
		if((b&1)==1)
			ans=mul(ans,a,mod);
		a=mul(a,a,mod);
		b>>=1;
	}
	//System.out.println("A::"+ans);
	return ans;
}
//N M L 长度 洗牌次数 访问的位置
//2^M*X=L(mod N+1)  2^M*x+(N+1)*Y=L
public static void main(String []args)
{
	Scanner cin=new Scanner (System.in);	
	long N,M,L;
	N=cin.nextLong();
	M=cin.nextLong();
	L=cin.nextLong();
	long A=quicks(2,M,N+1);
	long B=N+1;
	//System.out.println(A);
	long X=res(A,B);
	X=mul(X,L,B)%B;
	System.out.println(X);
	cin.close();
}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值