P11747 「TPOI-1A」鞋子特大号

· · 题解

Content

给定一个数 x,你可以对其进行以下操作若干次,直到无法再操作:

现在有以下两种询问共 T 个:

Solution

这两种询问都是要求出最多能进行多少次操作,而每次操作的 y 都不能为 x,所以 \gcd ( x , y ) 最大为 x 除去最小质因数,那么执行次数就为 x 质因子个数减一。

还需要注意:

第二个操作要用 unsigned long long。

Code

#include<bits/stdc++.h>
using namespace std;
int t,x,opt;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--){
        cin>>opt>>x;
        if(opt==1){
            int cnt=0;
            for(int i=2;i<=sqrt(x);i++)
                while(x%i==0){
                    cnt++;
                    x/=i;
                }
            if(x!=1)
                cnt++;
            cout<<max(0,cnt-1)<<endl;
        }
        else
            cout<<((unsigned long long)1<<x+1)<<endl;
    }
    return 0;
}