BJUTACM 吃飯的怪癖

2019ACM校賽的一個題,現場考慮的時候存了個素數數組,但由於沒有考慮大素數(10^7以上量級)導致蜜汁RE,查了半天也沒查出來...如果沒搞錯學校的電腦好像還是環境不太一樣,導致我輸入RE的數好像還能顯示結果,回來之後改了個算法終於過了...

 

數論題,大眼瞪小眼之後發現和歐拉函數有關,\frac{(\varphi (m)\cdot m)}{2}即爲結果,具體推導好像用到了最大公因數的性質

計算\varphi (m)可以使用連乘公式

現場還是心態不太一樣,大素數這個錯誤都沒有查出來,實在太菜了....

題目鏈接

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define ll long long

using namespace std;
bool judge(int a)
{
	if(a==1)
	{
		return 0;
	}
	if(a==2)
	{
		return 1;
	}
	else
	{
		for(int i=2;i<sqrt(a)+1;i++)
		{
			if(a%i==0)
			{
				return false;
			}
		}
		return true;
	}	
}


double euler(ll a)
{
	ll temp = a;
	double t = a;
	for(int i=2;i<sqrt(a)+1;i++)
	{
		if(temp%i==0&&judge(i)==1)
		{
			t *= ( 1- (1*1.0/i) ); 	
			while(temp%i==0)
			{
				temp /= i;//約掉所有這個素數 
			}
		}
	}
	if(judge(temp)==1)//如果現在還是個數 
	{
		t *= ( 1- (1*1.0/temp) );
 	} 
	if(temp==a)
	{
		temp = temp-1; 
		t = temp;
	}
//	cout<<t<<endl;
	return t;
}


int main(void)
{
	int a;
	while(cin>>a)
	{
		if(a==1)
		{
			cout<<1<<endl; 
			continue;	
		} 
		double temp = euler(a);		
		printf("%.0lf\n",temp*a*1.0/2);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章