C循环【扩展欧几里得】

    科技2024-04-19  97

    C循环(扩欧)

    题意

    思路

    那个n为存储,意味着每次膜除2^n,

    代码

    #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; typedef unsigned long long ull; #define mst(s,_s) memset(s, _s, sizeof(s)) const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int N = 1e6+100; int T,n,m; ll exgcd(ll a,ll b,ll &x,ll &y) { if(!b) { x=1,y=0; return a; } ll d=exgcd(b,a%b,y,x); y-=a/b*x; return d; } int main() { ll a,b,c,k; while(cin>>a>>b>>c>>k) { if(!a && !b && !c && !k) break; ll k1,k2; ll d=exgcd(c,1ll<<k,k1,k2); if((b-a)%d) cout<<"FOREVER"<<endl; else{ k1*=(b-a)/d; ll n=(1ll<<k) / d; cout<<((k1%n) + n)%n<<endl; } } return 0; }
    Processed: 0.012, SQL: 8