Link/Cut Tree CodeForces - 614A

拿到这道题一看,题意很简单,要你输出l,r里 k的power次,不存在的话输出-1;一开始简单的看了一下数据范围,感觉没什么问题。 后来发现,数据大一点的话很有可能就会溢出,那么再加个判断就可以解决溢出的问题。

/***If I get TLE , it is good.If I get AC,it's NICE !***/
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <sstream>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int INF=2147483647;
const int MAXN=1e5+10;
const ll mod=1e9+7;
using namespace std;
typedef long long ll;
int main(void)
{
    ll l, r,k;
    cin >> l >>r >>k;
    if(k>r && l==1)
    {
        printf("1\n");
        return 0;
    }
    ll x=1;
    int flag=0;
    while(x<=r)
    {
        if(x>=l && x<=r)
            {flag=1;break;}
         x*=k;
        if(x%k!=0)
            break;
    }
    if(flag==0)
    {
        printf("-1\n");
        return 0;
    }
    ll ans=1;
    int cnt=1;
    while(ans<=r)
    {
        if(ans>=l && ans<=r)
        {
            if(cnt==1)
            {
                printf("%I64d",ans);
                cnt=2;
            }
            else
                printf(" %I64d",ans);
        }
        ll t=ans;
        ans=ans*k;
        if(ans % t!=0)  break;
        if(ans%k!=0)
            break;
    }
    puts("");
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务