HDOJ 5150 Sum Sum Sum Miller_Rabin

很少有这么裸的题目,测一下Miller_Rabin

Sum Sum Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 72    Accepted Submission(s): 52

Problem Description

We call a positive number X P-number
if there is not a positive number that is less than X and
the greatest common divisor of these two numbers is bigger than 1.

Now you are given a sequence of integers. You task is to calculate the sum of P-numbers of the sequence.

Input

There are several test cases.

In each test case:

The first line contains a integer N(1≤N≤1000).
The second line contains N integers.
Each integer is between 1 and 1000.

Output

For each test case, output the sum of P-numbers of the sequence.

Sample Input

3
5 6 7
1
10

Sample Output

12
0

Source

BestCoder Round #24

/* ***********************************************
Author        :CKboss
Created Time  :2014年12月27日 星期六 21时51分17秒
File Name     :HDOJ5150.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

/// Miller_Rabin

typedef long long int LL;

const int S=8;///判断几次 (8~12)

/// (a*b)%c
LL mult_mod(LL a,LL b,LL c)
{
	a%=c; b%=c;
	LL ret=0; LL temp=a;
	while(b)
	{
		if(b&1)
		{
			ret+=temp;
			if(ret>c) ret-=c;
		}
		temp<<=1;
		if(temp>c) temp-=c;
		b>>=1LL;
	}
	return ret;
}

/// (a^n)%mod
LL pow_mod(LL a,LL n,LL mod)
{
	LL ret=1;
	LL temp=a%mod;
	while(n)
	{
		if(n&1) ret=mult_mod(ret,temp,mod);
		temp=mult_mod(temp,temp,mod);
		n>>=1LL;
	}
	return ret;
}

/// check a^(n-1)==1(mod n)
bool check(LL a,LL n,LL x,LL t)
{
	LL ret=pow_mod(a,x,n);
	LL last=ret;
	for(int i=1;i<=t;i++)
	{
		ret=mult_mod(ret,ret,n);
		if(ret==1&&last!=1&&last!=n-1) return true;
		last=ret;
	}
	if(ret!=1) return true;
	return false;
}

bool Miller_Rabin(LL n)
{
	if(n<2) return false;
	if(n==2) return true;
	if((n&1)==0) return false;
	LL x=n-1;
	LL t=0;
	while((x&1)==0) { x>>=1; t++;}
	srand(time(NULL));

	for(int i=0;i<S;i++)
	{
		LL a=rand()%(n-1)+1;
		if(check(a,n,x,t)) return false;
	}
	return true;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int n;
	while(scanf("%d",&n)!=EOF)
	{
		LL sum=0; LL x;
		for(int i=0;i<n;i++)
		{
			cin>>x;
			if(Miller_Rabin(x)||x==1)
			{
				sum+=x;
			}
		}
		cout<<sum<<endl;
	}

    return 0;
}
时间: 2024-10-03 23:16:10

HDOJ 5150 Sum Sum Sum Miller_Rabin的相关文章

leetcode第一刷_Combination Sum Combination Sum II

啊啊啊啊,好怀念这种用递归保存路径然后打印出来的题目啊,好久没遇到了. 分了两种,一种是可以重复使用数组中数字的,一种是每个数字只能用一次的.其实没有多大区别,第一种每次进入递归的时候都要从头开始尝试,第二种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样.跟我一样用引用保存中间结果的话,要注意回退的情况.第二种回退时,要把用到的那个数也恢复为可用,就完全像全排列时做的一样.破例贴两个题的代码,因为他们是在是不值得用两片文章来写. class Solution { publi

Path Sum,Path Sum II

Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below

32. Path Sum &amp;&amp; Path Sum II

Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and

有两个数组a,b,大小都为n,数组元素的值任意整形数,无序,通过交换a,b中的元素,使得|sum(a)-sum(b)|最小

有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 令A=sum(a)-sum(b) a的第i个元素和b的第j个元素交换后,a和b的和之差为 A'= sum(a) - a[i] + b[j] - (sum(b) - b[j] + a[i])           = sum(a) - sum(b) - 2 (a[i] - b[j])           = A - 2 (a[i] - b[j]) 设x = a[

有两个数组a,b,大小都为n,;通过交换a,b中的元素,使sum(a)-sum(b)最小。

有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为    A = sum(a) - sum(b) a的第i个元素和b的第j个元素交换后,a和b的和之差为    A' = sum(a) - a[i] + b[j] - (sum(b) - b[j] + a[i])           = sum(a) - sum(b) - 2 (a[i] - b[j])           = A

sum 除sum 还是 avg(字段/字段)

create table abc(baduser number(3),playuser number(3))                 ;SELECT t.*,rowid FROM abc t ;select sum(baduser)/sum(playuser),  --1          sum(baduser/playuser)/count(*),  --2           avg(baduser/playuser) --3from    abc; 2和3是结果一样的, 1不同

【HDOJ】3473 Minimum Sum

划分树解.主席树解MLE. 1 /* 3473 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque>

【HDOJ】P2058 The sum problem

题意很简单就是给你一个N和M,让你求在1-N的那些个子序列的值等于M 首先暴力法不解释,简单超时 再仔细想一想可以想到因为1-N是一个等差数列,可以运用我们曾经学过的只是来解决 假设开始的位置为s,结束的位置为t,那么一定要满足这个等式 (s+t)(t-s+1)=2*m 又因为S和T都是整数,所以左边的括号中每一项都是等式 所以s+t和t-s+1一定是2*m的因式 所以分解因式并带入就可以求出s和t 假设 s+t=a t-s+1=b a*b=2*m 解得 s=(a-b+1)/2 t=(a+b-1

【HDOJ】1244 Max Sum Plus Plus Plus

这题目一直wa,原来是因为我把JUDGE写错了,对拍了一下午都没检查出来.水DP啊. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 using namespace std; 7 8 #define MAXN 1020 9 #define MAXM 35 10 #define I