TLE on 11 求助
查看原帖
TLE on 11 求助
481851
Withers楼主2022/9/20 23:47

RT

#include<bits/stdc++.h>
//#include<bits/extc++.h>
#define Withers using
#define AK namespace
#define IOI std;
//#define ACM __gnu_pbds 
Withers AK IOI;
//Withers AK ACM;
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> Tree;
int n,m,u,v,w,x,y,z,l,r,minn=INT_MAX,maxx=INT_MIN,k;
int tst;
int a[200010];
char s[200010];
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
char t[200010];
#define infll 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define endl '\n'
static char buf[1<<18],*paa=buf,*pddd=buf;
static char buf2[1<<18],*pppp=buf2;
#define getchar() paa==pddd&&(pddd=(paa=buf)+fread(buf,1,1<<18,stdin),paa==pddd)?EOF:*paa++
inline void pc(char ch){
	if(pppp-buf2==1<<18) fwrite(buf2,1,1<<18,stdout),pppp=buf2;
	*pppp++=ch;
}
inline void pcc(){
	fwrite(buf2,1,pppp-buf2,stdout);
	pppp=buf2;
}
inline void rd(int &n){
	int w=1;
	register int x(0);register char c(getchar());
	while(c<'0'||c>'9'){if(c=='-') w=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
	n=w*x;return;
}
inline void write(int x){
	if(x<0) pc('-'),x=-x;
	static int sta[20];int top=0;
	do{sta[top++]=x%10,x/=10;}while(x);
	while(top) pc(sta[--top]+48);
}
inline void we(int x){
	write(x);
	pc('\n');
}
inline void ws(int x){
	write(x);
	pc(' ');
}
#define Y cout<<"YES"<<endl
#define N cout<<"NO"<<endl
#define deb(x) cout<<#x<<"="<<x<<" ";
#define pb push_back
#define fi first
#define se second
#define mx3(a,b,c) ((a>b?a:b)>c?(a>b?a:b):c)
#define mn3(a,b,c) ((a<b?a:b)<c?(a<b?a:b):c)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
int get(char s[])
{
	char ch='=';
	int cnt=0;
	int len=strlen(s+1);
	for(int i=1;i<=len;i++) s[i]=0;
	while(!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))) ch=getchar();
	while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9')) s[++cnt]=ch,ch=getchar();
	return cnt;
}
void put(char s[])
{
	int len=strlen(s+1);
	for(int i=1;i<=len;i++) pc(s[i]);
}
void get(string &s)
{
	s="";
	char ch='=';
	while(!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))) ch=getchar();
	while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9')) s.push_back(ch),ch=getchar();
	return;
}
void put(string s)
{
	int len=s.size();
	for(int i=0;i<len;i++) pc(s[i]);
}
void file(string s)
{
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}
namespace math
{
long long fac[1000010], inv[1000010];
int ask1[200010],ask2[200010];
int sc(int x,int y,int p)
{
	int ans=0;
	while(y)
	{
		if(y&1) ans+=x,ans%=p;
		y>>=1;
		x=x+x,x%=p;
	}
	return ans;
}
int gcd(int x, int y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}
int ksm(int a, int b, int p)
{
    int tmp = a, res = 1;
    while (b)
    {
        if (b & 1)
            res = 1ll * res * tmp % p;
        b >>= 1;
        tmp = 1ll * tmp * tmp % p;
    }
    return res;
}
int exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int d = exgcd(b, a % b, x, y);
    int tmp = x;
    x = y;
    y = tmp - a / b * y;
    return d;
}
int inv1(int a, int p)
{
    int x, y;
    int d = exgcd(a, p, x, y);
    if (d == 1)
        return (x % p + p) % p;
    else
        return -1;
}
void init(int n, int p)
{
    fac[1] = 1;
    for (int i = 2; i <= n; i++)
        fac[i] = 1ll*fac[i - 1] * i % p;
    inv[n] = inv1(fac[n], p);
    for (int i = n - 1; i >= 1; i--)
        inv[i] = 1ll*inv[i + 1] * (i + 1) % p;
}
int C(int n, int m, int p)
{
    if (m < 0 || m > n)
        return 0;
    if (m == 0 || m == n)
        return 1;
    return 1ll*fac[n] * inv[m] % p * inv[n - m] % p;
}
int  lucas(int n, int m, int p)
{
    if (m == 0)
        return 1;
    return C(n % p, m % p, p) * lucas(n / p, m / p, p) % p;
}
int excrt(int n)
{
	int lcm=ask1[1],now=ask2[1]%ask1[1],fail=0;
	int d;
	for(int i=2;i<=n;i++)
	{
		ask2[i]=(ask2[i]-now%ask1[i]+ask1[i])%ask1[i];
        d=exgcd(lcm,ask1[i],x,y);
        if(ask2[i]%d==0)k=sc(x,(ask2[i]/d),ask1[i]);
        else fail=1;
        now+=k*lcm;
        lcm=lcm/d*ask1[i];
        now=(now%lcm+lcm)%lcm;
	}
	if(fail) return -1;
	return now;
}
int sub1exlucas(int n,int p,int pk)
{
	if(n==0) return 1;
	int ans=1;
	for(int i=1;i<=pk;i++)
	{
		if(i%p) ans=ans*i%pk;
	}
	ans=ksm(ans,n/pk,pk);
	for(int i=1;i<=n%pk;i++)
	{
		if(i%p) ans=ans*i%pk;
	}
	return ans*sub1exlucas(n/p,p,pk)%pk;
}
int sub2exlucas(int n,int m,int p,int pk)
{
	if(n==0||m==0||n==m) return 1;
	if(n<m) return 0;
	int tp1=sub1exlucas(n,p,pk),tp2=sub1exlucas(m,p,pk),tp3=sub1exlucas(n-m,p,pk),cnt=0,tmp=n-m;
	while(n) n/=p,cnt+=n;
	while(m) m/=p,cnt-=m;
	while(tmp) tmp/=p,cnt-=tmp;
	return tp1*inv1(tp2,pk)%pk*inv1(tp3,pk)%pk*ksm(p,cnt,pk)%pk;
}
int exlucas(int n,int m,int p)
{
	int mx=sqrt(p);
	int cnt=0;
	for(int i=2;i<=mx&&p>=1;i++)
	{
		int pk=1;
		while(p%i==0) p/=i,pk*=i;
		if(pk>1) 
		{
			ask2[++cnt]=sub2exlucas(n,m,i,pk),ask1[cnt]=pk;
		}
	}
	if(p>1) ask2[++cnt]=sub2exlucas(n,m,p,p),ask1[cnt]=p;
	return excrt(cnt);
}
int bsgs(int a,int b,int p)
{
	unordered_map<int,int> mp;
	b%=p;
	int mx=sqrt(p)+1;
	for(int i=0;i<mx;i++)
	{
		mp[b*ksm(a,i,p)%p]=i;
	}
	a=ksm(a,mx,p);
	if(!a)
	{
		if(b==0) return 1;
		return -1;
	}
	for(int i=1;i<=mx;i++)
	{
		int tmp=ksm(a,i,p);
		auto it=mp.find(tmp);
		if(it==mp.end()) continue;
		if(i*mx-(*it).se>=0) return i*mx-(*it).se;
	}
	return -1;
}
int exbsgs(int a,int b,int p)
{
	a%=p,b%=p;
	if(b==1||p==1) return 0;
	int d=0,lc=1,cnt=0;
	while((d=__gcd(a,p))^1)
	{
		if(b%d) return -1;
		b/=d;p/=d;++cnt;
		lc=1ll*lc*(a/d)%p;
		if(lc==b) return cnt;
	}
	int x,y;
	exgcd(lc,p,x,y);
	int tmp=(x%p+p)%p;
	b=1ll*b*tmp%p;
	map<int,int> mp;
	int mx=sqrt(p)+1;
	for(int i=0;i<mx;i++)
	{
		mp[b*ksm(a,i,p)%p]=i;
	}
	a=ksm(a,mx,p);
	if(!a)
	{
		if(b==0) return 1+tmp;
		return -1;
	}
	int val=1;
	int tmpp=1;
	for(int i=0;i<=mx;i++)
	{
		auto it=mp.find(tmpp);
		tmpp*=a,tmpp%=p;
		if(it==mp.end()) continue;
		if(i*mx-(*it).se>=0) return i*mx-(*it).se+cnt;
	}
	return -1;
}
} 
using namespace math;
void solve()
{
	//do something
	while(1)
	{
		int a,b,p;
		rd(a);rd(p);rd(b);
		if(a==0&&p==0&&b==0) break;
		int ans=exbsgs(a,b,p);
		if(ans==-1) cout<<"No Solution\n";
		else cout<<ans<<'\n';
	}
}
void multi()
{
	//rd(tst);
	tst=1;
	while(tst--)
	{
		solve();
	}
	pcc();
}
signed main()
{
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	multi();
}
// POWERED BY WITHERS
// THINK ONCE, CODE TWICE
/*things to check
1.  int overflow or long long memory need
2.  recursion/array/binary search/dp/loop bounds
3.  precision
4.  special cases(n=1,bounds)
5.  delete debug statements
6.  initialize(especially multi-tests)
7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <= , - or =
8.  keep it simple and stupid
9.  do not delete, use // instead
10. operator priority
11. is there anything extra to output?
12. if you don't know where the bug is , try to clear some parts of the code
 and check each part seperately.
13. ...
*/
 
/* something to think about
1. greedy? dp? searching? dp with matrix/ segment tree? binary search?
2. If contains "not", why not 正难则反 or few affect?
*/
2022/9/20 23:47
加载中...