数论基础定理及其应用(hdu 2685 I won't tell you this is about number theory, hdu 2582 f(n),hdu 1792 A New Chan)

hdu 2685 I won’t tell you this is about number theory

定理:如果a>b,则

gcd(ambm,anbn)=agcd(n,m)bgcd(n,m);
对于这道题我们可以将b看做1,这样显而易见就可以得到结果了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

int Quick_pow(int a,int b,int k){
    int ans = 1;
    while(b){
        if(b&1) ans = (ans*a)%k;
        a = (a*a)%k;
        b >>= 1;
    }
    return ans;
}

int gcd(int a,int b){
    return (b == 0)?a:gcd(b, a%b);
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int a,m,n,k;
        scanf("%d%d%d%d", &a, &m, &n, &k);
        int ans = Quick_pow(a, gcd(m,n), k);
        printf("%d\n", (ans-1+k)%k);
    }
    return 0;
}

hdu 2582 f(n)

定理:

G=gcd(C1n+C2n,.....,+Cn1n);
(1) n位素数时,那么答案就是n.
(2) n有多个素因子,那么答案就是1.
(3) n只有一个素因子,那么答案就是这个素因子.
接下来打表解决问题,时间负责度很低。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000005;
typedef long long LL;
int vis[maxn];
LL f[maxn];

void Init(){
    int i,j;
    memset(vis, 0, sizeof(vis));
    int n = sqrt(maxn+0.5);
    for(i = 2; i <= n; i++) if(!vis[i])
        for(j = i*i; j < maxn; j += i)
            vis[j] = 1;
    for(i = 3; i < maxn; i++){
        if(!vis[i]) vis[i] = i;
        else{
            int temp = i;
            n = sqrt(temp+0.5);
            for(j = 2; j <= n; j++){
                if(temp%j == 0){
                    while(temp%j == 0)
                        temp /= j;
                    break;
                }
            }
            if(temp == 1)
                vis[i] = j;
            else vis[i] = 1;
        }
    }
    f[3] = vis[3];
    for(i = 4; i < maxn; i++)
        f[i] = vis[i]+f[i-1];
}

int main(){
    Init();
    int n;
    while(scanf("%d", &n) != EOF){
        printf("%I64d\n", f[n]);
    }
    return 0;
}

hdu 1792 A New Change Problem

定理:给定两个互素的正整数和,那么它们最大不能组合的数为

ABAB
,不能组合的数的个数为
(A1)(B1)/2

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

int main(){
    LL A,B;
    while(scanf("%I64d%I64d", &A, &B) != EOF){
        printf("%I64d %I64d\n", A*B-A-B, (A-1)*(B-1)/2);
    }
    return 0;
}

HDU 2769 Disgruntled Judge ##

这道题我们不知道a,b,但是我们知道a的范围,因此我们可以枚举a,并通过a得到b,怎么得到呢,因为
x2 = (a*x1+b)%10001;
x3 = (a*x2+b)%10001;
合并可得:x3 = (a^2*x1+ab+b)%10001;
这个式子中x1,x3,a都是已知的,因此可以直接用扩展欧几里得求解。然后依次递推出其他的x,同时进行判断,如果不满足那么a++,继续进行枚举即可,直到找到一个合适解然后跳出。本提还需要注意要用long long进行保存,不然会发生溢出。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 205;
const int MOD = 10001;  //这个不是质数
typedef long long LL;
LL num[maxn];

void exgcd(LL a, LL b, LL &d, LL &x, LL &y){
    if(!b) {x = 1; y = 0; d = a;return ;}
    else{
        exgcd(b, a%b, d, y, x);
        y -= x*(a/b);
    }
}

int main(){
 /*   #ifndef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL*/
    int T;
    scanf("%d", &T);
    for(int i = 1; i < 2*T; i += 2)
        scanf("%I64d", &num[i]);
    for(int a = 0; ; a++){
        LL d,x,y;
        LL cnt = a*a*num[1]-num[3];
        exgcd(MOD, a+1, d, x, y);
        if(cnt%d) continue;
        y = -y*cnt/d;
        int flag = 1;
        for(int i = 2; i <= 2*T; i++){
            if(i&1){
                if(num[i] != (a*num[i-1]+y)%MOD){
                    flag = 0;
                    break;
                }
            }
            else
                num[i] = (a*num[i-1]+y)%MOD;
        }
        if(flag) break;
    }
    for(int i = 2; i <= 2*T; i += 2)
        printf("%I64d\n", num[i]);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值