BZOJ 3122 SDOI2013 随机数生成器

 公式就不推了.hzwer上的很清楚.

 值得注意的一点是,如果最后答案成0,需要加上mod.否则400ms wa.

 

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<map>
 5 
 6 using namespace std;
 7 #define ll long long
 8 #define FILE "dealing"
 9 #define up(i,j,n) for(int i=j;i<=n;i++)
10 #define db long double 
11 #define pii pair<int,int>
12 #define pb push_back
13 #define mem(a,L) memset(a,0,sizeof(int)*(L+1))
14 template<class T> inline bool cmin(T& a,T b){return a>b?a=b,true:false;}
15 template<class T> inline bool cmax(T& a,T b){return a<b?a=b,true:false;}
16 template<class T> inline T squ(T a){return a*a;}
17 const ll maxn=2000100+10,MAXN=20200,limit=1e7,base=23;
18 int read(){
19     int x=0,f=1,ch=getchar();
20     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
21     while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
22     return x*f;
23 }
24 ll mod,a,b,x1,t;
25 void exgcd(ll a,ll b,ll& d,ll& x,ll& y){
26     if(b==0){d=a;x=1,y=0;return;}
27     exgcd(b,a%b,d,x,y);
28     ll t=x;
29     x=y;
30     y=t-a/b*y;
31 }
32 ll qpow(ll a,ll b){
33     ll ans=1;
34     while(b){
35         if(b&1)ans=ans*a%mod;
36         a=a*a%mod;
37         b>>=1;
38     }
39     return ans;
40 }
41 ll BSGS(ll a,ll b){
42     map<ll,int> t;
43     ll m=(int)sqrt(mod+1.0)+1;
44     ll inv=qpow(qpow(a,m),mod-2);
45     ll w=1;
46     for(int i=0;i<m;i++)
47         t[w]=i,w=(w*a)%mod;
48     for(int i=0;i<=m;i++){
49         if(t.count(b))return t[b]+i*m;
50         b=b*inv%mod;
51     }
52     return -2;
53 }
54 int main(){
55     freopen(FILE".in","r",stdin);
56     freopen(FILE".out","w",stdout);
57     int T=read();
58     while(T--){
59         mod=read(),a=read(),b=read(),x1=read(),t=read();
60         if(t==x1){printf("1\n");continue;}
61         else if(a==0){printf("%lld\n",b==t?2LL:-1LL);continue;}
62         else if(a==1){
63             ll x,y,d,c=(t-x1+mod)%mod;
64             exgcd(b,mod,d,x,y);
65             if(c%d){printf("-1\n");continue;}
66             x=x*(c/d);
67             x=(x%mod+1+mod)%mod;
68             if(x==0)x=mod;
69             printf("%lld\n",x);
70         }
71         else {
72             ll c=qpow(a-1,mod-2),x,y,d,sheng=(t+b*c%mod)%mod;
73             exgcd((x1+b*c%mod)%mod,mod,d,x,y);
74             if(sheng%d){printf("-1\n");continue;}
75             x=x*(sheng/d)%mod;
76             x=(x%mod+mod)%mod;
77             ll ans=(BSGS(a,x)+1)%mod;
78             if(ans==0)ans+=mod;
79             printf("%lld\n",ans);
80         }
81     }
82     return 0;
83 }
View Code

 

转载于:https://www.cnblogs.com/chadinblog/p/6812253.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值