Prime Number (CodeForces-359C) [fast power / thinking]

The meaning of problems: Known X, the array arr [n], a fraction of the molecule seeking the greatest common factor the denominator. Molecule is ΣX ^ arr [i], the denominator of X ^ Σarr [i], the array is not decremented sequence.

Ideas: the game when that came up with the right ideas, WA lost a lot of hair, looked at the code written by someone else only to find themselves left out many details.

  1. easy to think, that is the lowest power of molecules may be needed to answer

  2. Due to the presence of the same number in arr, so various molecules may be present combined power situation similar items, it should be fully completed before combining like terms, then proceeding to step 1.

  3. a trick, not all items required to complete the merger, if and only if the current coefficient minimum term of X can be divisible only need to continue to merge, otherwise the number of the current item is the answer to the required number.

Of particular note are: the completion of the merger, the number of molecules in the lowest power is likely greater than the denominator, so it should take the number as the answer to the lesser of the two.

code show as below:

#include<cstdio>
#include<iostream>
using namespace std;
const int mo=1e9+7;
int x;
int mpow(long long xx,long long nn){
    long long res=1;
    while(nn!=0){
        if(nn&1){
            res=res*xx%mo;
        }
        nn>>=1;
        xx=xx*xx%mo;
    }
    return res;
}

int main(){
    int n;
    long long num=0,arr[100010];
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;i++){
        scanf("%I64d",&arr[i]);
        num+=arr[i];
    }
    for(int i=1;i<=n;i++){
        arr[i]=num-arr[i];
    }
    for(int i=1;i<=(n/2);i++){
        swap(arr[i],arr[n-i+1]);
    }
    int sum=1;
    long long ans;
    arr[n+1]=-1;
    for(int i=2;i<=n+1;i++){
        if(arr[i]==arr[i-1]){
            sum++;
        }
        else {
            if(sum%x==0){
                sum/=x;
                arr[--i]++;
            }
            else {
                ans=arr[i-1];
                break;
            }
        }
    }
    ans=min(ans,num);
    printf("%d",mpow(x,ans));
    return 0;
}
By xxmlala

 

Guess you like

Origin www.cnblogs.com/xxmlala-fff/p/11622006.html