同余问题(1)

同余: ,当且仅当 即a,b模m同余。

同余问题基础定理:

如果a,b,c是整数,m是正整数,且 那么有:  ; 

设a,b,c,d是整数,m是正整数,如果 ,有:  ,

同余的对称性:

同余的传递性:if a,b and c are integers with

推导:

取模mod和取余%的区别:前者的结果符号和mod后的数字一样,后者的结果符号和%前的数字一样。
例如:-8%7=-1 (计算机程序直接输出-1)
-8 mod 7= 6 
8%-7=1 
8 mod -7=-6

简单的模同余问题:
http://poj.org/problem?id=2115
poj C Looooops(同余方程)
大意:问for (variable = A; variable != B; variable += C)statement; 的A,B,C在mod(2^k)下经过几次能执行完,不能满足终止退奥简单的话就输出forever.
由题意可以列出等式:



拓展欧几里得求出最小的执行次数x。

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
    if(b==0){
        x=1; y=0; d=a;
        return ;
    }
    exgcd(b,a%b,d,y,x);
    y=y-(a/b)*x;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    LL a,b,c,k;
    while((cin>>a>>b>>c>>k)&&(a+b+c+k)){
        LL aa=c,bb=(LL)1<<k,cc=b-a,d,x,y;  //1必须先转化过来
        exgcd(aa,bb,d,x,y);
        bool has=1;
        if(cc%d) has=0;
        x=(x+bb)%bb;
        x=x*(cc/d);
        LL t=bb/d; 
        x=(x%t+t)%t; //运算次数不超过bb/d次,不然会超过1<<k。
        if(!has) puts("FOREVER");
        else printf("%lld\n",x);
    }
    return 0;
}

http://poj.org/problem?id=2769

pku  2769  Reduced ID Numbers(同余)
核心语句: For each group, she wants to find the smallest positive integer m, such that within the group all SINs reduced modulo m are unique.

Sample Input

2
1
124866
3
124866
111111
987651

Sample Output

1
8
注意本题可能超时的部分,要巧妙的清空数据hash思想,不能直接用memset。
TLE ||-_-

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e6+5;
int a[305];
bool f[maxn];
int main()
{
    //freopen("cin.txt","r",stdin);
    int t,n;
    cin>>t;
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        if(n==1) { puts("1"); continue ;}
        int k;
        for(k=2;;k++){
            bool find=1;
            memset(f,0,sizeof(f));
            for(int i=0;i<n;i++){
                if(f[a[i]%k]){ find=0; break; }
                f[a[i]%k]=1;
            }
            if(find) break;
        }
        printf("%d\n",k);
    }
    return 0;
}

修改后:
#include <iostream>
#include <cstdio>
#include <cstring>  //直接使用memset超时。
using namespace std;
const int maxn=1e6+5;
int a[305];
int que[305],top;
bool f[maxn];
void clear(){
    for(int i=0;i<top;i++){
        f[que[i]]=0;
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int t,n;
    cin>>t;
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        int k;
        for(k=1;;k++){
            bool find=1;
            //memset(f,0,sizeof(f));
            clear();
            top=0;
            for(int i=0;i<n;i++){
                if(f[a[i]%k]){ find=0; break; }
                que[top++]=a[i]%k;
                f[a[i]%k]=1;
            }
            if(find) break;
        }
        printf("%d\n",k);
    }
    return 0;
}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值