Y sequenceTime
limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1192 Accepted Submission (s): 265
Problem Descriptionyellowstar likes integers so much the he listed all positive integers in ascending order,but he hates Those numbers which can be written as A^b (A, B is positive integers,2<=b<=r), so he removed them all. Yellowstar calls the sequence that formed by the rest integers "Y sequence". When R=3,the first few items of it is:
2,3,5,6,7,10 ...
Given positive integers n and r,you should output Y (n) (the n-th number of y sequence. It is obvious this Y (1) =2 whatever R is).
Inputthe first line of the input contains a single number t:the number of test cases.
Then T-cases follow, each contains the positive integer n and r described above.
n<=2*10^18,2<=r<=62,t<=30000.
Outputfor each case,output Y (n).
Sample Input
210 210 3
Sample Output
1314
Authorfzuacm
Source2015 multi-university Training Contest 1
Recommendwe carefully selected several similar problems for you:5390 5389 5388 5387 5386
Mobius Function + Iteration
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)); #define INF (2139062143) #define F (100000007) #define MAXT (30000+10) #define MAXR (a) typedef long long L L;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (A-b+llabs (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} Const long Long prime[] = {2,3,5, 7,11,13,17,19,23,29, 31,37,41,43,47,53,59,61,67, 71,73,79,83,89,97}; Class Math{public:ll gcd (ll a,ll b) {if (!b) return A;return gcd (b,a%b);} ll ABS (ll x) {if (x>=0) return x;return-x;} ll exgcd (ll A,ll b,ll &x, ll &y) {if (!b) {X=1,y=0;return A; } ll G=EXGCD (b,a%b,x,y); ll T=x;x=y;y=t-a/b*y; return g; } ll Pow2 (ll a,int b,ll p)//a^b mod p {if (b==0) return 1; if (b==1) return A; ll C=pow2 (a,b/2,p); c=c*c%p; if (b&1) c=c*a%p; return C; } ll MODP (ll a,ll b,ll p)//a*x=b (mod p) {ll x, y; ll G=EXGCD (a,p,x,y), D; if (b%g) {return-1;} D=b/g;x*=d,y*=d; X= (X+abs (x)/p*p+p)%p; return x; } ll Mobius (ll x) {}}s;ll n;int r;int num[600000],nnum,mobnum[600000],rnnum[600000];void get () {nnum=0;for (int i=0;prime [i]<=maxr;i++) {int m=nnum; for (j,m) {if (PRIME[I]*NUM[J]<=MAXR) {num[++nnum]=prime[i]*num[j]; mobnum[nnum]=-mobnum[j];}} Num[++nnum] = Prime[i];mobnum[nnum]=-1;rnnum[i]=nnum;}} LL Calc (ll x) {ll ans=0;int rn=0;while (prime[rn+1]<=r) ++rn; for (I,rnnum[rn]) {ans+=-mobnum[i]* (LL) (POW (x+0.1,1.0/num[i])-1);} return x-ans-1;} ll Y (ll N,int R) {ll Ans=n;while (1) {ll tmp=calc (ans); if (tmp==n) break;ans+=n-tmp;} return ans;} int main () {//freopen ("j.in", "R", stdin),//freopen (". Out", "w", stdout); get ();//for (I,nnum) cout<<num[i]< < ' <<mobnum[i]<<endl;int t;scanf ("%d", &t), while (t--) {scanf ("%lld%d", &n,&r);p rintf ("% Lld\n ", Y (N,r));} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5297 (Y Sequence-mobius function tolerance + iteration)