当前位置:   article > 正文

【AtCoder】C - Unexpressed 二分+思维

c - unexpressed

C

1010的数据量,我竟然傻傻的想着打表。
实际上二分,枚举i的范围从2到sqrt(n),把其所有ix次方存进set;
输出n-s.size();

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb push_back
#define fi first
#define se second
#define mem(a,x) memset(a,x,sizeof(a));
#define db double 
#define fir(i,a,n) for(int i=a;i<=n;i++)
const int inf=0x3f3f3f3f;
const int MOD=1e9+7;
//======================
const int N=2e5+10;
ll n,ans;

ll find_b(ll x)
{
//	int l=1,r=35;
	ll ans=0;
	ll t=x;
	for(int i=1;i<=35;i++)
	{
		if(1ll*t*x>n) 
		{
			ans=i;break;
		}
		else t*=x;
	}
	return ans;
}
set<ll>s;

int main()
{
	cin>>n;
	ans=n;
	for(ll i=2;i*i<=n;i++)
	{
		if(s.count(i)) continue;
		ll t=find_b(i);	
		ll tt=i*i;
		for(ll j=2;j<=t;j++)
		{
			s.insert(tt);
			//cout<<tt<<endl;
			tt*=i;
		}
	}
	ans=n-s.size();
	cout<<ans;
	return 0; 
}
/*
//	set<ll>::iterator it;
//	for(it=s.begin();it!=s.end();it++)
//	{
//		cout<<*it<<endl;
//	}
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

后来发现不用二分找边界,直接乘就是了,超过就跳出循环。
大佬代码:AtCoder

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
int main()
{
    vector<bool> aa(1e5+5);
    cin>>n;
    ll ans=n;
    for (ll i=2;i*i<=n;i++)
    {
        if (aa[i]==1)
            continue;
        ll temp=i*i;
        while (temp<=n)
        {
            ans--;
            if (temp<aa.size())
                aa[temp]=1;
            temp*=i;
        }
    }  
    cout<<ans<<endl;
   
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

我真傻,真的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/52699
推荐阅读
相关标签
  

闽ICP备14008679号