数论模板

5 篇文章 0 订阅

判断两数是否互质:

#include<iostream>  
using namespace std;  
  
bool isCoprime(int x,int y)  
{  
    if(x==1 && y==1)//1和1互质  
        return true;  
    else if(x<=0 || y<=0 || x==y)//非正整数都不存在互质的说法  
        return false;  
    else if(x==1 || y==1)//1和任何正整数都互质  
        return true;  
    else  
    {  
        int tmp=0;  
        //使用求商判断法,如果输入的x<y,第一次循环会交换x和y的位置  
        while(true)  
        {  
            tmp=x%y;  
            if(tmp==0)  
            {  
                break;  
            }  
            else  
            {  
                x=y;  
                y=tmp;  
            }  
        }  
        if(y==1)          //最大公约数为1,所以互质  
            return true;  
        else              //最大公约数大于1,所以不互质  
            return false;  
  
    }  
}  
  
int main(void)  
{  
    bool ret=isCoprime(19,6);  
  
    cout<<ret<<endl;  
    return 0;  
}  

Leading and Trailing

Description

You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).

Output

For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.

Sample Input

5

123456 1

123456 2

2 31

2 32

29 8751919

Sample Output

Case 1: 123 456

Case 2: 152 936

Case 3: 214 648

Case 4: 429 296

Case 5: 665 669

解题思路:求出n^k的前3位和后三位;

求最后的三位,可以通过直接取余(快速幂)得到;求前三位则需要一些数学知识对于给定的一个数n,它可以写成10^a,其中这个a为浮点数,则n^k=(10^a)^k=10^a*k=(10^x)*(10^y);其中x,y分别是a*k的整数部分和小数部分,对于t=n^k这个数,它的位数由(10^x)决定,它的位数上的值则有(10^y)决定,因此我们要求t的前三位,只需要将10^y求出,在乘以100,就得到了它的前三位。

n^k=a.bc*10^m ( m为n^k的位数,即m=(int)lg(n^k)=(int)(k*lgn) );

            求对数:  k*lgn=lg(a.bc)+m

             即 a.bc=10^(k*lgn-m)=10^(k*lgn-(int)(k*lgn));

              abc=a.bc*100;



[cpp]  view plain  copy
  1. #include<cstdio>  
  2. #include<math.h>  
  3. typedef long long ll;  
  4. ll n,k;  
  5.   
  6. ll qpow(ll n,ll k)  
  7. {  
  8.     ll res=1;  
  9.     while(k>0)  
  10.     {  
  11.         if(k%2==1)   
  12.            res=(res%1000)*(n%1000)%1000;  
  13.         n=(n%1000)*(n%1000)%1000;  
  14.         k=k/2;  
  15.     }  
  16.     return res%1000;  
  17. }  
  18.   
  19. ll f(ll n)  
  20. {  
  21.     double x=k*log10(n)-(int)(k*log10(n));  
  22.     return pow(10,x)*100;  
  23. }  
  24. int main()  
  25. {  
  26.     int T;  
  27.     scanf("%d",&T);  
  28.     int tag=1;  
  29.     while(T--)  
  30.     {  
  31.         scanf("%lld%lld",&n,&k);  
  32.         printf("Case %d: %lld %03lld\n",tag++,f(n),qpow(n,k));  
  33.     }  
  34.     return 0;  
  35. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值