2019 Multi-University Training Contest 3

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int S=8;

ll mult_mod(ll a,ll b,ll c)
{
    a%=c;
    b%=c;
    ll ret=0,tmp=a;
    while (b)
    {
        if (b&1)
        {
            ret+=tmp;
            if (ret>c)
            {
                ret-=c;
            }
        }
        tmp<<=1;
        if (tmp>c) tmp-=c;
        b>>=1;
    }
    return ret;
}

ll pow_mod(ll a,ll n,ll mod)
{
    ll ret=1;
    ll temp=a%mod;
    while (n)
    {
        if(n&1)
        {
            ret=mult_mod(ret,temp,mod);
        }
        temp=mult_mod(temp,temp,mod);
        n>>=1;
    }
    return ret;
}

bool check(ll a,ll n,ll x,ll t)
{
    ll ret=pow_mod(a,x,n);
    ll last=ret;
    for (int i=1; i<=t; i++)
    {
        ret=mult_mod(ret,ret,n);
        if (ret==1&&last!=1&&last!=n-1)
        {
            return 1;
        }
        last=ret;
    }
    if (ret!=1)
    {
        return 1;
    }
    return 0;
}

bool Miller_Rabin(ll n)
{
    if (n<2)
    {
        return 0;
    }
    if (n==2)
    {
        return 1;
    }
    if ((n&1)==0)
    {
        return 0;
    }
    ll x=n-1;
    ll t=0;
    while ((x&1)==0)
    {
        x>>=1;
        t++;
    }
    srand(time(NULL));
    for (int i=0; i<S; i++)
    {
        ll a=rand()%(n-1)+1;
        if (check(a,n,x,t))
        {
            return 0;
        }
    }
    return 1;
}

ll factor[100];
int tol;

ll gcd(ll a,ll b)
{
    if (!b)
    {
        return a;
    }
    return gcd(b,a%b);
}
ll pollard_rho(ll x,ll c)
{
    ll i=1,k=2;
    srand(time(NULL));
    ll x0=rand()%(x-1)+1;
    ll y=x0;
    while (1)
    {
        i++;
        x0=(mult_mod(x0,x0,x)+c)%x;
        ll d=gcd(y-x0,x);
        if (d!=1&&d!=x)
        {
            return d;
        }
        if (y==x0)
        {
            return x;
        }
        if (i==k)
        {
            y=x0;
            k+=k;
        }
    }
}

__int128 quick(__int128 a,__int128 b,__int128 p)
{
    __int128 ret=1%p;
    while (b)
    {
        if (b&1)
        {
            ret=ret*a%p;
        }
        b>>=1;
        a=a*a%p;
    }
    return ret;
}

void findfac(ll n,ll k)
{
    if (n==1)
    {
        return;
    }
    if (Miller_Rabin(n))
    {
        factor[tol++]=n;
        return;
    }
    ll p=n;
    ll c=k;
    while (p>=n)
    {
        p=pollard_rho(p,c--);
    }
    findfac(p,k);
    findfac(n/p,k);
}

__int128 inv(__int128 a,__int128 p){
    return quick(a,p-2,p);
}
int main()
{
    ll t,p,prime;
    __int128 ans;
    scanf("%lld",&t);
    while (t--)
    {
        scanf("%lld",&p);
        for (ll i=p-1; i>=2; i--)
        {
            if (Miller_Rabin(i))
            {
                prime=i;
                break;
            }
        }
        ans=p-1;
        for (__int128 i=p-1;i>prime;i--){
            ans=ans*inv(i,p)%p;
        }
        printf("%lld\n",(ll)ans);
    }
}

  

原文地址:https://www.cnblogs.com/Accpted/p/11265726.html

时间: 2024-10-11 07:21:54

2019 Multi-University Training Contest 3的相关文章

2019 HDOJ Multi-University Training Contest Stage 8(杭电多校)

中规中矩的一场. 题目链接:http://acm.hdu.edu.cn/contests/contest_show.php?cid=855 C: 定义函数f(d,k)为数字d在数字k中出现的次数.给定d和x,找到尽量大的k使得k<=x且f(d,k)==k. 很诡异的一题,最好的做法仍然是打表找规律.题解给了一个很神奇的结论:满足条件的k<1011且k的分布非常稀疏. 1 /* basic header */ 2 #include <bits/stdc++.h> 3 /* defin

2019 HDOJ Multi-University Training Contest Stage 10(杭电多校)

最后一场多校打得一般般. 题目链接:http://acm.hdu.edu.cn/contests/contest_show.php?cid=857 C: E: I: BFS水题. 1 /* Codeforces Contest 2019_mutc_10 2 * Problem I 3 * Au: SJoshua 4 */ 5 #include <queue> 6 #include <cstdio> 7 #include <vector> 8 #include <s

2019 HDOJ Multi-University Training Contest Stage 2(杭电多校)

服务器时不时爆炸,有点难受. 题目链接:http://acm.hdu.edu.cn/userloginex.php?cid=849 A: 神仙题.不可做题. B: dp. C: 推式子题. D: 边分治. E: 可以数学推理的题.但是显然打表更快找出规律.对打出来的结果做两次差分即可. 1 /* basic header */ 2 #include <bits/stdc++.h> 3 /* define */ 4 #define ll long long 5 #define dou doubl

2019 HDOJ Multi-University Training Contest Stage 4(杭电多校)

很抱歉过了这么多天才补这场,最近真的挺忙的…… 出题人是朝鲜的(目测是金策工业?),挺难. 题目链接:http://acm.hdu.edu.cn/contests/contest_show.php?cid=851 A: 签到题. 对于当前的点,若其编号为偶数,则可与1相连使得边权贡献为0.否则从低位向高位找当前点编号的二进制表示的第一个0,使这个0变为1,其他位置变为0并检查新的数字是否小于等于n.若小于等于n则贡献为0,反之贡献为1. 1 /* basic header */ 2 #inclu

2019 Nowcoder Multi-University Training Contest 1 H-XOR

由于每个元素贡献是线性的,那么等价于求每个元素出现在多少个异或和为$0$的子集内.因为是任意元素可以去异或,那么自然想到线性基.先对整个集合A求一遍线性基,设为$R$,假设$R$中元素个数为$r$,那么任取一个不在$R$内的元素,$R$中肯定存在一种取法能和这个元素异或和为$0$.同理,取定一个不在$R$内的元素,再随便取另外任意个不在$R$内的元素,$R$内仍然存在一种取法使得这个异或和为$0$.那么每个不在$R$内的元素包含在$2^{n - r - 1}$个集合内(其他不在$R$内的元素可以

HDU校赛 | 2019 Multi-University Training Contest 3

2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 1004. Distribution of books 考虑二分答案,设当前二分出来的是\(x\). 设\(f_i\)表示前\(i\)个能分成最多的段数,使得每一段和都\(\leqslant x\). 转移显然,枚举一个\(j\),若\(s_i-s_j\leqslant x\)则转移,\(s_i\)表示前

[最短路,最大流最小割定理] 2019 Multi-University Training Contest 1 Path

题目:http://acm.hdu.edu.cn/showproblem.php?pid=6582 Path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3747    Accepted Submission(s): 1075 Problem Description Years later, Jerry fell in love

HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 5943    Accepted Submission(s): 2004 Problem Description Before

2014 Multi-University Training Contest 6 Apple Tree(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 120 Problem Description I've bought an orchard an