HDU5377

The meaning of problems: a sum, m groups interrogation, each x, y find \ (X ^ t = Y \ MOD P, P | sum \) , P is a prime number, find the smallest t
Solution: first processing sum of all prime factors p, p obtained primitive root RT, \ (RT a = X ^ \ MOD p, RT = Y ^ B \ MOD p \) , \ (^ {RT} = a * T RT ^ B \ MOD p \) ,
\ (T = a * B \ P-MOD. 1 \) , the first pre-bsgs table, then ab & determined, and then seek exgcd t, find the minimum value

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
//#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=mul(ans,a,c);a=mul(a,a,c),b>>=1;}return ans;}

using namespace std;
//using namespace __gnu_pbds;

const ld pi=acos(-1);
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=1000000+7,maxn=2000000+10,inf=0x3f3f3f3f;

struct prime{
    int p,rt;

    struct HashTable
    {
        int top, head[N];
        struct Node
        {
            int x, y, next;//x是原根p的幂,y是幂次数,next是y+1次幂所在的head桶
        }node[N];
        void init()
        {
            top = 0;
            memset(head, 0, sizeof(head));
        }
        void Insert(int x, int y)
        {
            node[top].x = x; node[top].y = y; node[top].next = head[x%N];
            head[x%N] = top++;
        }
        int Find(int x)//求出x是原根proot的几次幂
        {
            for (int tx = head[x%N]; tx; tx = node[tx].next)
            if (node[tx].x == x)return node[tx].y;
            return -1;
        }
    }ma;

    void init()
    {
        ma.init();
        int a=rt;
        int m=100000,now=1;
        ma.Insert(now,0);
        for(int i=1;i<=m;i++)
        {
            now=(1ll*now*a)%p;
            ma.Insert(now,i);
        }
    }
    int query(int b)
    {
        int m=100000;
        int ans=-1,t=qp(rt,m,p),now=qp(b,p-2,p);
        for(int i=1;i<=m;i++)
        {
            now=1ll*now*t%p;
            int te=ma.Find(now);
            if(~te)
            {
                ans=i*m-te;
                break;
            }
        }
        if(ans!=-1)ans=(ans%p+p)%p;
        return ans;
    }
}p[15];
vi v;
int findroot(int n)
{
    int p=n-1;v.clear();
    for(int i=2;i*i<=p;i++)if(p%i==0)
    {
        v.pb(i);
        while(p%i==0)p/=i;
    }
    if(p!=1)v.pb(p);
    for(int i=1;i<=n;i++)
    {
        bool ok=1;
        for(int j=0;j<v.size();j++)
            if(qp(i,(n-1)/v[j],n)==1)
                ok=0;
        if(ok)return i;
    }
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b){x=1,y=0;return a;}
    ll ans=exgcd(b,a%b,x,y);
    ll t=x;x=y;y=t-a/b*y;
    return ans;
}
int main()
{
    int t,cas=0;scanf("%d",&t);
    while(t--)
    {
        int cnt=0;
        int s,m;scanf("%d%d",&s,&m);
        for(int i=2;i*i<=s;i++)if(s%i==0)
        {
            p[++cnt].p=i,p[cnt].rt=findroot(i),p[cnt].init();
            while(s%i==0)s/=i;
        }
        if(s!=1)p[++cnt].p=s,p[cnt].rt=findroot(s),p[cnt].init();
        printf("Case #%d:\n",++cas);
        for(int o = 1; o <= m; o++)
        {
            int x,y,ans=inf;scanf("%d%d",&x,&y);
            for(int i=1;i<=cnt;i++)
            {
                int a=p[i].query(x),b=p[i].query(y);
                assert(a!=-1&&b!=-1);
                if(b%gcd(a,p[i].p-1)!=0)continue;
                else
                {
                    ll c,d;
                    exgcd(a,p[i].p-1,c,d);
                    c=c*b/gcd(a,p[i].p-1);
                    ll te=(p[i].p-1)/gcd(a,p[i].p-1);
                    c=(c%te+te)%te;
                    ans=min(ans,(int)c);
                }
            }
            printf("%d\n",ans==inf?-1:ans);
        }
    }
    return 0;
}
/********************
1
175 2
2 3
********************/

Guess you like

Origin www.cnblogs.com/acjiumeng/p/11201812.html