【牛客 - 327牛客寒假算法基础集训营2 I】处女座的测验(二)(积性函数性质,数论,素数唯一性分解,STL)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/87908569

题干:

链接:https://ac.nowcoder.com/acm/contest/327/I
来源:牛客网
 

现在处女座顺利的完成了测验,处女座想要知道知道自己输出的结果是否正确。他希望知道自己有自己输出的数中有多少对是不满足要求的。

更具体的,处女座想知道下面程序段的答案

int main()

{

         int n;

         cin>>n;

         for (int i=1;i<=n;i++)

                   cin>>a[i];

         int ans=0;

         for (int i=1;i<=n;i++)

         {

                   for (int j=i+1;j<=n;j++)

                   {

                            if(τ(a[i]∗a[j])≤10)ans=ans+1;if(τ(a[i]∗a[j])≤10)ans=ans+1;

                   }

         }

         cout<<ans<<endl;

         return 0;

}

其中为n的因子的个数

输入描述:

 

两行

第一行一个整数n

第二行n个整数,a1,a2,…,an

2<=n<=2000, 1<=ai<=3*108

输出描述:

一行,一个整数ans

示例1

输入

复制

7
34 45 23 12 63 23 90

输出

复制

3

备注:

不保证任意两个整数互质

解题报告:

  直接暴力求解,因为该范围内的数分解成素数最多就9个,再加上大于4个就剪枝,所以n^2log10完全不会超时。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e4 + 5;
int n,a[MAX];
//vector<int> vv[MAX];
map<int,int> mp[MAX];
void ff(int id,int x) {
	for(int i = 2; i*i<=x; i++) {
		int cnt = 0;
		if(x%i == 0) {
			while(x%i==0) x/=i,cnt++; 
			mp[id][i] = cnt;
		}
	}
	if(x > 1) mp[id][x] = 1;
}
int main() 
{
	cin>>n;
	for(int i = 1; i<=n; i++) scanf("%d",a+i),ff(i,a[i]);
	int ans = 0;
	for(int i = 1; i<=n; i++) {
		if(mp[i].size() >= 4) continue;
		for(int j = i+1; j<=n; j++) {
			if(mp[j].size() >= 4) continue;
			int nums = 1;
			for(auto it : mp[i]) {
				int fac = it.first;
				int num = it.second;
				if(mp[j].find(fac) != mp[j].end()) {
					nums *= (num + mp[j][fac]+1);
				}
				else nums *= (num+1); 
			}
			for(auto it : mp[j]) {
				int fac = it.first;
				int num = it.second;
				if(mp[i].find(fac) == mp[i].end()) nums *= (num+1);
			}
			if(nums <= 10) ans++;
		}
	} 
	printf("%d\n",ans);
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/87908569