今日 SGU 5.6
SGU 106
题意:问你有多少个<x,y>,满足ax+by+c=0,x1<=x<=x2,y1<=y<=y2
收货:拓展欧几里得求解的是这种方程,ax+by=1,gcd(a,b)=1
如果gcd(a,b)不等于1的话,那么你直接传进egcd函数里求出的x,y还是a1x+b1y=1的解,a1=a/gcd(a,b),b1=b/gcd(a,b)
还有注意y1,x0,y0会和系统的里面变量冲突
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 1e5+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //inv[1]=1; //for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; void egcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b) d=a ,x=1 ,y=0 ; else{ egcd(b,a%b,d,y,x); y-=a/b*x; } } ll x,y,d; ll a,b,c,x1,x2,yy1,yy2; int main(){ scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&x1,&x2,&yy1,&yy2); c=-c; if(c<0) a=-a,b=-b,c=-c; if(a<0) a=-a,swap(x1,x2),x1=-x1,x2=-x2; if(b<0) b=-b,swap(yy1,yy2),yy1=-yy1,yy2=-yy2; if(a==0||b==0){ if(a==0&&b==0){ if(c!=0) puts("0"); else printf("%lld\n",(x2-x1+1)*(yy2-yy1+1)); }else if(a==0){ if(c%b==0&&c/b>=yy1&&c/b<=yy2) printf("%lld\n",x2-x1+1); else return puts("0"),0; }else { if(c%a==0&&c/a>=x1&&c/a<=x2) printf("%lld\n",yy2-yy1+1); else puts("0"); } return 0; } egcd(a,b,d,x,y);//这边求得是a/d*x+b/d*y=1(d=gcd(a,b))的解,那么乘以c/gcd(a,b)就得到a/d*x+b/d*y=c/d的一个解了 if(c%d) return puts("0"),0; double aa = a/d,bb = b/d; ll cc = c/d; x*=cc,y*=cc; ll r=min(floor((x2-x)/bb),floor((y-yy1)/aa)) ,l=max(ceil((x1-x)/bb),ceil((y-yy2)/aa)); if(r>=l) printf("%lld\n",r-l+1); else puts("0"); return 0; }
SGU 111
题意:求一个大整数的开方
收获:大整数开方模板
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 1e5+5; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //inv[1]=1; //for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; int l; int work(int o,char *O,int I){//求大整数开根号 char c, *D=O ; if(o>0){ for(l=0;D[l];D[l++]-=10) { D[l++]-=120; D[l]-=110; while(!work(0,O,l)) D[l]+=20; putchar((D[l]+1032)/20); } putchar(10); } else{ c=o+(D[I]+82)%10-(I>l/2)*(D[I-l+I]+72)/10-9; D[I]+=I<0 ? 0 : !(o=work(c/10,O,I-1))*((c+999)%10-(D[I]+92)%10); } return o; } int main(){ char s[maxn];s[0]='0'; scanf("%s",s+1); if(strlen(s)%2==1) work(2,s+1,0); else work(2,s,0); return 0; }
//转载别人的手算开方方法,防止自己以后忘记
SGU 181
题意求:xk,然后给你个x的递推公式
收获:找循环节,你要记录循环节的头
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int maxn = 1e3+6; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll mod; ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } ll x[maxn]; map<ll,int> m; int main(){ qc; ll a,alpha,beta,gamma,k; cin>>a>>alpha>>beta>>gamma>>mod>>k; x[0]=a; if(k==0) { cout<<a<<endl; return 0; } int t=mod,be=0; rep(i,1,k+1) { x[i]=(x[i-1]*x[i-1]*alpha+beta*x[i-1]+gamma)%mod; if(m.count(x[i])) { t = i - m[x[i]]; be = m[x[i]]; break; } m[x[i]] = i; } cout<<x[be+(k-be)%t]; return 0; }