poj_2115C Looooops(模线性方程)
2016-07-16 08:01
239 查看
题目链接:http://poj.org/problem?id=2115
题意:定义一个循环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
C Looooops
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 CTU Open 2004 |
求循环执行的次数,如果死循环输出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
//求模线性方程 #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define ll long long ll gcd(ll a, ll b, ll &x, ll &y) { if(b==0) { x = 1; y = 0; return a; } ll ans = gcd(b,a%b,x,y); ll tx = x; x = y; y = tx-a/b*y; return ans; } int main() { ll a,b,c,k; while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&k)) { if(a==b&&b==c&&c==k&&k==0) return 0; ll M = 1LL << k; b = b-a; ll m,n; ll d = gcd(c,M,m,n); if(b%d!=0) { puts("FOREVER"); continue; } ll ans = (m*(b/d))%M; ans = (ans%(M/d)+M/d)%(M/d); printf("%lld\n",ans); } return 0; } /* void gcd(LL a, LL b, LL &d, LL &x, LL &y) { if(!b) { d = a; x = 1; y = 0; } else { gcd(b, a%b, d, y, x); y-= x*(a/b); } } */
相关文章推荐
- runLoop机制
- Uboot与Linux之间的参数传递
- Linux之用户管理
- nginx错误日志级别
- linux和Windows下文本格式转换
- VMware安装win7提示 operating system not found
- 购物网站
- PHP网站开发指引
- javaEE:day1-验证码生成技术和启动tomcat的四种方式
- 网站tomcat配置
- 安装与配置Tomcat8
- 使用ssh将linux目录挂到window下
- MySQL的一条慢SQL查询导致整个网站宕机的解决方法
- MySQL OOM 系列一 Linux内存分配
- linux环境下zookeeper部署
- install ubuntu-restricted-extras
- 把tomcat服务添加进系统服务中
- 运维学习路线图
- linux获取系统时间
- linux常用命令显示说明