zoukankan      html  css  js  c++  java
  • D

    题目链接:http://codeforces.com/problemset/problem/17/D

    Nick is attracted by everything unconventional. He doesn't like decimal number system any more, and he decided to study other number systems. A number system with base b caught his attention. Before he starts studying it, he wants to write in his notepad all the numbers of length n without leading zeros in this number system. Each page in Nick's notepad has enough space for c numbers exactly. Nick writes every suitable number only once, starting with the first clean page and leaving no clean spaces. Nick never writes number 0 as he has unpleasant memories about zero divide.

    Would you help Nick find out how many numbers will be written on the last page.

    Input

    The only input line contains three space-separated integers bn and c (2 ≤ b < 101061 ≤ n < 101061 ≤ c ≤ 109). You may consider that Nick has infinite patience, endless amount of paper and representations of digits as characters. The numbers doesn't contain leading zeros.

    Output

    In the only line output the amount of numbers written on the same page as the last number.

    Examples

    Input
    2 3 3
    Output
    1
    Input
    2 3 4
    Output
    4

    Note

    In both samples there are exactly 4 numbers of length 3 in binary number system. In the first sample Nick writes 3 numbers on the first page and 1 on the second page. In the second sample all the 4 numbers can be written on the first page.

    题意:ans =  (b-1)*b^(n-1)%c,强调,为了b^(n-1)

    思路:欧拉降幂的板子题,放个公式:

    解题的时候只要用下面两条公式就行了,原因我也不清楚,就判断一下指数和模数的大小关系,选择用第二个和第三个公式就行

    看代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<map>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<map>
    #include<queue>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ull;
    #define sc1(a) scanf("%lld",&a)
    #define pf1(a) printf("%lld
    ",a)
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    const LL INF=1e18;
    const ull base=2333;
    const int maxn=1e2+55;
    const int maxm=1e4+50;
    const int maxv=1e6+5;
    const int mod=51123987;
    LL Cal(string s,LL p)
    {
        LL len=s.size();
        LL sum=0;
        for(LL i=0;i<len;i++)
        {
            sum=(sum*10+s[i]-'0')%p;
        }
        return sum;
    }
    LL phi(LL n)
    {
        LL res=n,m=n;
        for(LL i=2;i<=sqrt(m);i++)
        {
            if(m%i==0) res=res/i*(i-1);
            while(m%i==0) m/=i;
        }
        if(m>1) res=res/m*(m-1);
        return res;
    }
    LL Quick_pow(LL x,LL y,LL p)
    {
        LL ans=1,res=x;
        while(y)
        {
            if(y&1) ans=ans*res%p;
            res=res*res%p;
            y>>=1;
        }
        return ans;
    }
    /**
    (b-1)*b^(n-1)%c
    */
    int main()
    {
        string b,n;
        LL c;
        cin>>b>>n>>c;
    
        LL ph=phi(c);
        LL bb=Cal(b,c);
        LL nn=Cal(n,ph);
        if(n.size()<10)//如果指数和模数都在十次方之内 直接算
        {
            LL z=0;
            for(LL i=0;i<n.size();i++) z=(z*10+n[i]-'0');
            LL ans=((bb-1+c)%c)*Quick_pow(bb,z-1,c)%c;
            if(ans==0) ans=c;
            pf1(ans);return 0;
        }
        
        //如果指数大于十次方 肯定大于模数 套用公式
        nn=(nn-1+ph)%ph+ph;
        LL ans=((bb-1+c)%c)*Quick_pow(bb,nn,c)%c;
        if(ans==0) ans=c;
        pf1(ans);
        return 0;
    }
    /**
    
    */
  • 相关阅读:
    const char* && string && String^ 类型转换
    vs2010 vc++ 统一修改所有工程的目录配置
    OSG开发概览
    osg渲染数据高程文件
    postgis数据库文件shapefile导入 dbf file (.dbf) can not be opened.shapefile import failed.
    Point ZM 转换为Point 类型
    Extjs关于alert显示不出—异步问题
    js执行顺序<转>
    HTML中IFrame父窗口与子窗口相互操作
    SpringMVC学习系列(6) 之 数据验证
  • 原文地址:https://www.cnblogs.com/caijiaming/p/12539811.html
Copyright © 2011-2022 走看看