1 solutions

  • 0
    @ 2024-6-1 16:20:35

    暴力解法(80pts)

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int n;
    	cin>>n;
    	map<int,int> m;
    	for(int j=2;j<=n;j++)
    	{
    		int a=j;
    		for(int i=2;i<=a/i;i++)
    		{
    			while(a%i==0)
    			{
    				m[i]++;
    				a/=i;
    			}
    		}
    		if(a>1) m[a]++;
    	}
    	for(auto it:m)
    	{
    		cout<<it.first<<" "<<it.second<<endl;
    	}
    	return 0;
    }
    

    线性筛质数

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e6+10;
    vector<int> p;
    bool st[N];
    void get(int n) //线性筛质数
    {
        for(int i=2;i<=n;i++)
        {
            if(!st[i]) p.push_back(i);
            for(int j=0;p[j]<=n/i;j++)
            {
                st[i*p[j]]=1;
                if(i%p[j]==0)
                {
                    break;
                }
            }
        }
    }
    int main()
    {
        int n;
        cin>>n;
        get(n);
        map<int,int> h;
        for(int i=0;i<p.size();i++)
        {
            int t=p[i];
            for(int j=n;j;j/=t) //求n的阶乘里面有多少个t
            {
                h[t]+=j/t;
            }
        }
        for(auto it:h)
        {
            cout<<it.first<<" "<<it.second<<endl;
        }
        return 0;
    }
    
    • 1

    Information

    ID
    183
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    (None)
    # Submissions
    29
    Accepted
    6
    Uploaded By