zoukankan      html  css  js  c++  java
  • bzoj 3884: 上帝与集合的正确用法

    参考:http://blog.csdn.net/sinat_27410769/article/details/46754209
    首先看一下欧拉定理及扩展(还不会证先坑着

    [a^nequiv a^{n\%phi(p)}\%p,[gcd(n,p)==1] ]

    [a^n=a^{n\%phi(p)+phi(p)}\%p,[gcd(n,p)==1] ]

    然后回到题目,首先把模数拆成( p=2^kq )的形式,发现这时q一定为奇数,满足互质条件
    那么题目中的式子就可以通过欧拉公式转换为:

    [2^k(2^{(2^{2^{2^{...}}}-k)\%phi(q)+phi(q)}\%q) ]

    递归求解,发现( phi(p) )每次至少缩小一倍,所以总的递归深度是( log_2p )
    phi值用的时候直接求会比线性块很多,因为次数大约是( sqrt{p}log_2p )

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int T,n;
    long long phi(int x)
    {
    	int re=x;
    	for(int i=2;i*i<=x;i++)
    		if(x%i==0)
    		{
    			re-=re/i;
    			while(x%i==0)
    				x/=i;
    		}
    	return x>1?re-re/x:re;
    }
    long long ksm(long long a,long long b,long long p)
    {
    	long long r=1ll;
    	while(b)
    	{
    		if(b&1)
    			r=r*a%p;
    		a=a*a%p;
    		b>>=1;
    	}
    	return r;
    }
    long long f(int x)
    {
    	if(x==1)
    		return 0;
    	int p=phi(x);
    	return ksm(2,f(p)+p,x);
    }
    int main()
    {
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&n);
    		printf("%lld
    ",f(n));
    	}
    	return 0;
    }
    
  • 相关阅读:
    request、bs4爬虫
    1031 查验身份证
    1029 旧键盘
    1028 人口普查
    1027 打印沙漏
    1026 程序运行时间
    1025 反转链表
    1024 科学计数法
    1022 D进制的A+B
    1021 个位数统计
  • 原文地址:https://www.cnblogs.com/lokiii/p/8295908.html
Copyright © 2011-2022 走看看