zoukankan      html  css  js  c++  java
  • poj_2115C Looooops(模线性方程)

    题目链接:http://poj.org/problem?id=2115

    C Looooops
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 22912   Accepted: 6293

    Description

    A Compiler Mystery: We are given a C-language style for loop of type 
    for (variable = A; variable != B; variable += C)
    
    statement;

    I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k

    Input

    The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2k) are the parameters of the loop. 

    The input is finished by a line containing four zeros. 

    Output

    The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate. 

    Sample Input

    3 3 2 16
    3 7 2 16
    7 3 2 16
    3 4 2 16
    0 0 0 0
    

    Sample Output

    0
    2
    32766
    FOREVER

    Source

    题意:定义一个循环for(int i = A ; i!=B ; i= (i+c)%2^k)

    求循环执行的次数,如果死循环输出forever;

    题解:上面的循环可以写成(A+C*X)%2^k=B%2^k

    上式可以写成 C*X%2^k=B-A%2^k;

    这样就转化成了一个模线性方程。

    模线性方程有下列定理

    数论:

    求解模线性方程

    • 定理:方程ax=b(mod n)对于未知量x有解,当且仅当gcd(a, n)|b
    • 定理:方程ax=b(mod n)或者对模n有d个不同的解,其中d=gcd(a, n)或者无解。
    • 定理:设d=gcd(a, n),假定对整数x’和y’,有d=ax’+ny’。如果d|b,则方程ax=b(modn)有一个解的值为x0,满足x0=x’(b/d)mod n
    • 定理:假设方程ax=b(mod n)有解(即有d|b,其中d=gcd(a, n)),x0是该方程的任意一个解,则该方程对模n恰有d个不同的解,分别为:xi=x0+i(n/d)(i = 1, 2, …, d-1)
    • int Modular_Linear(int a,int b,int n)
      {
         int d,x,y,x0,i;
         d=Extend_Euclid(a,n,x,y);
         if(b%d==0)
         {
             x0=(x*(b/d))%n;
             if(x0<n)x0+=n;
             for(i=0;i<d;i++)cout<<(x0+i*n/d)%n<<endl;
         }
         return 0;
      }
    •  

     注意:这个题要求如果有解的话输出最小解,一般在处理最小解的时候用(x%mod+mod)%mod

     1 //求模线性方程
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 #define ll long long
     8 
     9 ll gcd(ll a, ll b, ll &x, ll &y)
    10 {
    11     if(b==0) {
    12         x = 1;
    13         y = 0;
    14         return a;
    15     }
    16     ll ans = gcd(b,a%b,x,y);
    17     ll tx = x;
    18     x = y;
    19     y = tx-a/b*y;
    20     return ans;
    21 }
    22 int main()
    23 {
    24     ll a,b,c,k;
    25     while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&k))
    26     {
    27         if(a==b&&b==c&&c==k&&k==0) return 0;
    28         ll M = 1LL << k;
    29         b = b-a;
    30         ll m,n;
    31         ll d = gcd(c,M,m,n);
    32         if(b%d!=0) {
    33             puts("FOREVER");
    34             continue;
    35         }
    36         ll ans = (m*(b/d))%M;
    37         ans = (ans%(M/d)+M/d)%(M/d);
    38         printf("%lld
    ",ans);
    39     }
    40     return 0;
    41 }
    42 /*
    43 void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
    44     if(!b) { d = a; x = 1; y = 0; }
    45     else { gcd(b, a%b, d, y, x); y-= x*(a/b); }
    46 }
    47 */
  • 相关阅读:
    CH the luckiest number 欧拉函数 同余
    bzoj1257余数求和 数论分块 暴力
    luogup1463 反素数
    CH3101 阶乘分解
    T10396 曹老板斗地主(中度模拟)
    NOIP 2015子串(DP)
    CF1205B Shortest Cycle(Floyd判最小环)
    P2055 [ZJOI2009]假期的宿舍(二分图匹配)
    灾后重建(最短路)
    CF1098A Sum in the tree(贪心)
  • 原文地址:https://www.cnblogs.com/shanyr/p/5675242.html
Copyright © 2011-2022 走看看