• 暑假前挑战赛1—— A,B题解


    A - A^B Mod C

    给出3个正整数A B C,求A^B Mod C。
     
    例如,3 5 8,3^5 Mod 8 = 3。
    Input
    3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)
    Output
    输出计算结果
    Sample Input
    3 5 8
    Sample Output
    3


    解题思路:
    1.题目数据较大,所以计算乘方的时候直接用for或while循环会超时
    这里我们采用快速幂算法
    Ps:贴上百度百科快速幂代码
    int pow(int a,int b)
    {
        int r=1,base=a;
        while(b!=0)
        {
            if(b%2)  r*=base;
            base*=base;
            b/=2;
        }
        return r;
    }

    2.此题在快速幂过程中即可取模。

    附上代码

    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    
    long long A,B,C;//int应该不行,所以此处采用long long
    long long p;
    
    long long ksm(long long a,long long b,long long c)//即a^b Mod c
    {
        long long ans=1;
        a=a%c;
        while(b>0)
        {
            if(b%2==1)
            {
                ans=(ans*a)%c;
            }
            b/=2;
            a=(a*a)%c;
        }
        return ans;
    }
    int main()
    {
        scanf("%lld%lld%lld",&A,&B,&C);
        printf("%lld",ksm(A,B,C));
        return 0;
    }

    B - 2 3 5 7的倍数

     给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数。 例如N = 10,只有1不是2 3 5 7的倍数。

    Input

    输入1个数N(1 <= N <= 10^18)。

    Outpu

    t输出不是2 3 5 7的倍数的数共有多少。

    Sample Input

    10

    Sample Output

    1

    此题的关键是要去掉重复算出的倍数

    如果只求2 3 5 7的倍数那么其中会有重复例如6的倍数10的倍数等

    即网上说的容斥原理

    引用其理解:

    先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复,这种计数的方法称为容斥原理--简而言之,就是对于重叠次数只有奇数次的,我们加上,重叠次数为偶数次的,我们要减去。

    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    
    long long N;
    long long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o;
    
    int main()
    {
        scanf("%lld",&N);
        a=N/2;//此处求倍数的个数,不需要for枚举
        b=N/3;  
        c=N/5;  
        d=N/7;  
        e=N/6;  
        f=N/10;  
        g=N/14;  
        h=N/15;  
        i=N/21;  
        j=N/35;  
        k=N/30;  
        l=N/42;  
        m=N/70;  
        n=N/105;  
        o=N/210;
        long long cnt=(a+b+c+d)-(e+f+g+h+i+j)+(k+l+m+n)-o;//加单减双
        printf("%lld",N-cnt); 
        return 0;
    }
  • 相关阅读:
    Understand each field of AWR [ID 884046.1]
    Linux iptables 防火墙 添加删除 端口
    SCSI 总线和协议
    How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
    How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
    Linux iptables 防火墙 添加删除 端口
    Oracle alert log 按天 存放 脚本
    文件系统 和 网络附接存储(NAS: Network Attached Storage)
    SCSI 总线和协议
    IP(Internet Protocal) 地址 说明
  • 原文地址:https://www.cnblogs.com/wanghaixv/p/9128575.html
Copyright © 2020-2023  润新知