zoukankan      html  css  js  c++  java
  • 【BZOJ】【2480】【SPOJ 3105】Mod

    扩展BSGS


      Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了。。。

      扩展BSGS模板题

     1 /**************************************************************
     2     Problem: 2480
     3     User: Tunix
     4     Language: C++
     5     Result: Accepted
     6     Time:6948 ms
     7     Memory:2212 kb
     8 ****************************************************************/
     9  
    10 //BZOJ 2480
    11 #include<cstdio>
    12 #include<cmath>
    13 #include<map>
    14 #include<cstring>
    15 #include<cstdlib>
    16 #include<iostream>
    17 #include<algorithm>
    18 #define rep(i,n) for(int i=0;i<n;++i)
    19 #define F(i,j,n) for(int i=j;i<=n;++i)
    20 #define D(i,j,n) for(int i=j;i>=n;--i)
    21 #define pb push_back
    22 using namespace std;
    23 typedef long long LL;
    24 inline int getint(){
    25     int r=1,v=0; char ch=getchar();
    26     for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-1;
    27     for(; isdigit(ch);ch=getchar()) v=v*10-'0'+ch;
    28     return r*v;
    29 }
    30 const int N=100010;
    31 /*******************template********************/
    32  
    33 int a,p,b;
    34  
    35 inline int gcd(int a,int b){return b ? gcd(b,a%b) : a;}
    36 LL Pow(LL a,LL b,LL P){
    37     LL r=1;
    38     for(;b;b>>=1,a=a*a%P) if (b&1) r=r*a%P;
    39     return r;
    40 }
    41 map<int,int>mp;
    42 int ex_BSGS(int a,int p,int b){
    43     int d=0,res=0,cnt=0;
    44     LL t=0;
    45     a%=p; b%=p;
    46     for(int i=0,j=1;i<=50;i++,j=(LL)j*a%p) if (j==b) return i;
    47     t=1%p;
    48  
    49     while((d=gcd(a,p))!=1){
    50         if (b%d) return -1;
    51         p/=d; b/=d; t=t*a/d%p;
    52         ++cnt;
    53     }
    54     mp.clear();
    55     int m=ceil(sqrt(p));
    56     for(int i=0,j=1;i<m;i++,j=(LL)j*a%p) mp[(LL)b*j%p]=i;
    57     a=Pow(a,m,p);
    58     for(int i=0,j=t;i<=m;i++,j=(LL)j*a%p)
    59         if (i && mp.count(j))
    60             return i*m-mp[j]+cnt;
    61     return -1;
    62 }
    63 int main(){
    64 #ifndef ONLINE_JUDGE
    65     freopen("2480.in","r",stdin);
    66     freopen("2480.out","w",stdout);
    67 #endif
    68     int ans;
    69     while(scanf("%d%d%d",&a,&p,&b)!=EOF && a && p && b){
    70         ans=ex_BSGS(a,p,b);
    71         if (ans>=0) printf("%d
    ",ans);
    72         else puts("No Solution");
    73     }
    74     return 0;
    75 }
    View Code

    2480: Spoj3105 Mod

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 248  Solved: 103
    [Submit][Status][Discuss]

    Description

    已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x。
     

    Input

        每个测试文件中最多包含100组测试数据。
        每组数据中,每行包含3个正整数a,p,b。
        当a=p=b=0时,表示测试数据读入完全。
     

    Output

     
        对于每组数据,输出一行。
        如果无解,输出“No Solution”(不含引号),否则输出最小自然数解。
     

    Sample Input


    5 58 33
    2 4 3
    0 0 0

    Sample Output


    9
    No Solution

    HINT



        10%的数据,a,p,b≤10000;

        对于另外30%的数据,p为质数;

        100%的数据,a,p,b≤1e9。

    Source

    [Submit][Status][Discuss]
  • 相关阅读:
    【网摘】Data Warehousing and BI Introduction
    【网摘】OLAP and Business Intelligence (ROLAP vs. MOLAP vs. HOLAP)
    [Oracle Issues]Wrong Password for User for Host Credentials in EM
    [Oracle Utility] Adrian Billington’s data_dump
    [Oracle Data Cartridge Interface] UserDefined Aggregation Functions
    【网摘】MVP (Passive View and Supervising Controller)
    SQL Techniques – Columns to Rows, Rows to Columns
    [Oracle Mgmt] Query Archivelog Mode, Change Archivelog Dest, etc.
    [Oracle SQL]Greatest and Least
    [Oracle 9i] Case Expression and Case Statement in 9i
  • 原文地址:https://www.cnblogs.com/Tunix/p/4550145.html
Copyright © 2011-2022 走看看