1. 程式人生 > >SDUT 3258 Square Number(2015年山東省第六屆ACM大學生程式設計競賽)

SDUT 3258 Square Number(2015年山東省第六屆ACM大學生程式設計競賽)

Square Number

Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^

題目描述

In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinct integers (a1, a2, ..., an), you need to find the number of pairs (ai, aj) that satisfy (ai * aj) is a square number.

輸入

 The first line of the input contains an integer T (1 ≤ T ≤ 20) which means the number of test cases. Then T lines follow, each line starts with a number N (1 ≤ N ≤ 100000), then N integers followed (all the integers are between 1 and 1000000).

輸出

 For each test case, you should output the answer of each case.

示例輸入

1   
5   
1 2 3 4 12

示例輸出

2

分析:

判斷兩個數的乘積是否為平方數,需要先將該數字拆分多個素數的平方數的形式,例如:

12 = 2*2*3

3 = 3

這樣2*2就可以忽略不看,因為它已經是平方數,只需要找3與12的質因數3配成平方數即可.

先打一個1000的素數表,因為最大資料是1e6,所以取根號大小就行。然後根據素數表打一個平方數表。

程式碼如下:

#include <stdio.h>
#include <string.h>
#define MAX 1000010
int pri[1010];
int dp[200];
int vis[MAX];
int amount=0;
void init()
{
	for(int i=2;i<=1000;i++)
	{//素數打表 
		for(int j=i+i;j<=1000;j+=i)
		{
			if(!pri[j])
				pri[j]=1;
		}
	}
	//平方數打表 
	for(int i=2;i<=1000;i++)
		if(!pri[i])
			dp[amount++]=i*i;
}

int main()
{
	int T,n;
	int t;
	int i,j;
	init();
	scanf("%d",&T);
	while(T--)
	{
		memset(vis,0,sizeof(vis));
		int ans=0;
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d",&t);
			for(j=0;j<amount;j++)
			{
				if(t%dp[j]==0)
				{
					while(t%dp[j]==0)
						t/=dp[j];
				}
			}
			ans+=vis[t];
			//除去平方因數後的值 ,有多少個該平方因數的值就可以組成多少對 
			vis[t]++;
		}
		printf("%d\n",ans);
	}
	return 0;
}


相關推薦

SDUT 3258 Square Number(2015山東省ACM大學生程式設計競賽)

Square Number Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^ 題目描述 In mathematics,

2015山東省ACM大學生程式設計競賽-B-Lowest Unique Price

Link: http://www.sdutacm.org/sdutoj/problem.php?action=showproblem&problemid=3252 Lowest Unique Price Time Limit: 1000ms

“浪潮杯”山東省ACM大學生程式設計競賽I

Fascinated with the computer games, Gabriel even forgets to study. Now she needs to finish her homework, and there is an easy problem: f(n)= She is requir

“浪潮杯”山東省ACM大學生程式設計競賽 J

sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51. 01 #include <iostream>

山東省ACM大學生程式設計競賽 總結

很惋惜的一場比賽,比賽前我想了很多個結局,可能是銀牌中等,銀牌末尾,要是運氣好的可能能混一個金牌回來。。但是銀首這個真的是超乎我意料之外。。。 開始比賽的不久我就看到了K題,一個作為簽到題的存在,開始習慣性的先敲輸入輸出。寫完輸入輸出之後感覺沒有比較好寫的思路

Fruit Ninja II(山東省ACM大學生程式設計競賽

Have you ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for Nvidia Tegra 2 based A

山東省ACM大學生程式設計競賽 Fruit Ninja I(01揹包)

01揹包 加上vector的運用,貪心sort排序 #include <stdio.h> #include <vector> #include <string.h>

2017河南省ACM大學生程式設計競賽總結

河南第十屆大學生程式設計競賽總結             比賽過程隊友部落格有記錄:http://blog.csdn.net/dreamNYC/article/details/71417450             主要就總結下這次比賽的感受:                     我們隊雖然和

[2012山東省ACM大學生程序設計競賽]——Mine Number

== memory cat chan get iostream sts 設計 target Mine Number 題目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&proble

2017山東省acm省賽總結

        作為一個即將退役的大三隊員,這屆省賽對我來說就是最後一次比賽了,同時也是最重要的一次比賽。在去年的省賽中,我們隊發揮的不理想,沒拿到獎牌,因此今年就是最後的機會,我們隊在這一年中也準備了很多,其中也有過放棄,失望等時期,不過好在最後都堅持下來了。其中對我來說

2010山東省第一ACM大學生程式設計競賽——Greatest Number

Greatest Number 題目意思:給你一串數,任取四個,可以重複,求最大值,但不能超過m。 可以先將任意兩個數相加,然後再二分查詢求出符合題意的最大值即可。 #include<i

沒有什麼不可能—記山東省ACM程式設計競賽(退役總結帖)

          大一下學期,第一次聽說了ACM這個詞,當時每週六也開設了培訓課,但我好像一次也沒有去過,當時對這個詞並沒有什麼太大的印象。後來學院裡引進了自己的OJ,那時候我連基本的輸入輸出格式都不懂,當經歷了一

山東省第一ACM大學生程式設計競賽 Phone Number 字典樹

Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^ 題目描述 We know that if a phone number A is another phone number B’s

山東省ACM省賽 Lowest Unique Price(模擬)

Problem Description Recently my buddies and I came across an idea! We want to build a website to sell things in a new way. For ea

福建省大學生程式設計競賽-FZU 2280 HASH處理+暴力搜尋

題目:Problem 2280 Magic  Problem Description Kim is a magician, he can use n kinds of magic, number from 1 to n. We use string Si to describe m

福建省大學生程式設計競賽-FZU 2277 DFS +線段樹+讀入掛

FZU 2277  Problem 2277 Change Accept: 245    Submit: 1186 Time Limit: 2000 mSec    Memory Limit : 262

ACM趣味程式設計競賽四場(正式賽)A B C

Yitong_Qin and Xiaoyu_Chen are playing a game.There are $N$ stones placed on the ground,forming a sequence. Thr stones are labeled from 1 to N. Yitong_Qin

ACM趣味程式設計競賽三場(正式賽)官方題解

Source:LinPC 題解:首先預處理統計a-z字元的數量,根據漸變字串的定義,由於總字元數較小,所以可以列舉漸變字串的起點,然後找到由該起點起始的最大漸變字串,然後從統計的字元中刪除所用字元,如此迴圈操作至剩餘字元數為0即可 題解: 易得,只要最長的那條

電子科技大學ACM趣味程式設計競賽第二場題解

A陰陽師?這遊戲沒有ssr! 題解: ans = sigma(1 - (1 - Pi/100)^ 2); 標程: #include <stdio.h> #include <stdlib.h> int main() {     int N,p;    

福建省大學生程式設計競賽-重現賽(感謝承辦方廈門理工學院)

Pro.A Frog fzu2272 雞兔同籠 #include<stdio.h> int main() { int t,a,b; scanf("%d",&am