2013 acm 长沙网络赛 G题 素数+枚举 Goldbach
题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3856
先预处理求出两个素数的和与积,然后枚举n-prime和n/prime的情况。
表达式可能的情况 a a*b a+b a+b+c a*b*c a*b+c (注意没有(a+b)*c的情况)
对于a*b和a+b的判重 只需要控制 a<=b的范围即可
对于a*b+c的情况 不存在重复情况
对于a+b+c a*b*c
分三种情况
①a!=b && b!=c 这种情况对于全枚举会出现三次 分别是a+b+c a+c+b b+c+a
②a==b && b!=c 这种情况对于全枚举会出现二次 分别是a+b+c a+c+b
③a==b && b==c 这种情况对于全枚举会出现一次 分别是a+b+c
为了能够去重 可以考虑将②③情况补全成三次 然后sum除了3即可
除以3的求模需要注意不能直接mod,需要模3*mod
130ms 代码,8s的时限太给力了。。
#include <stdio.h>
#include<math.h>
#include<string.h>
#define bigt long long
#define N 80005
const int M=;
int a[N],p[N];
int pp[N]={};
int pp2[N]={};
int pnum;
bigt sum =;
bigt sum1 =;
bigt sum2 = ;
const bigt mod = 3000000021LL;
const int mod2 = ;
void Prime() {
int i, j;
memset(a, , N*sizeof(a[]));
pnum = ;
for(i = ; i < N; ++i) {
if(!a[i]) a[i] = p[pnum++] = i;
for(j = ; j < pnum && i * p[j] < N; ++j) {
a[i*p[j]] = p[j];
if(!(i%p[j])) break;
}
}
} void init()
{
int i=,j;
do{
for(j=i;j<pnum;j++)
{
if(p[i]+p[j]>M)
break;
else
{
pp[p[i]+p[j]] ++;
}
}
if(p[i]<=sqrt(M*1.0))
for(j=i;j<pnum;j++)
{
if(p[i]*p[j]>M)
break;
else
{
pp2[p[i]*p[j]] ++;
}
}
i++;
}while(i<pnum);
} int main()
{
int n;
Prime();
init();
while(scanf("%d",&n)!=EOF)
{
sum = sum1 = sum2 = ;
if(a[n]==n) sum2++; for(int i=;p[i]<n;i++)
{
int st = p[i];
int ed = n - p[i];
if(ed%== && a[ed/]==ed/)
{
sum1++;
if(ed/==st)
sum1++;
} sum1 = sum1 + pp[ed];
sum2 = sum2 + pp2[ed]; if(a[ed]==ed && *st<=n) sum2++;
if(sum1>mod) sum1 -= mod;
if(sum2>mod2) sum2 -= mod2;
} sum = (sum1/ + sum2 )%mod2;
sum1=sum2=; for(int i=;p[i]<n-;i++)
{
if(!(n%p[i]))
{
int st = p[i];
int ed = n / p[i];
double tmp = sqrt(ed)+1e-;
int tmp2 = (int)tmp; if(tmp2*tmp2==ed && a[tmp2]==tmp2)
{
sum1++;
if(tmp2==st)
sum1++;
} sum1 = (sum1 + pp2[ed]);
if(a[ed]==ed && st*st<=n)
sum2++; if(sum1>mod)
sum1 -= mod;
}
}
sum = sum + (sum1/ + sum2 )%mod2;
printf("%lld\n",sum%mod2);
}
return ;
}
2013 acm 长沙网络赛 G题 素数+枚举 Goldbach的更多相关文章
- 2013长沙网络赛H题Hypersphere (蛋疼的题目 神似邀请赛A题)
Hypersphere Time Limit: 1 Second Memory Limit: 32768 KB In the world of k-dimension, there's a ...
- 2013 ACM/ICPC 长沙网络赛J题
题意:一个数列,给出这个数列中的某些位置的数,给出所有相邻的三个数字的和,数列头和尾处给出相邻两个数字的和.有若干次询问,每次问某一位置的数字的最大值. 分析:设数列为a1-an.首先通过相邻三个数字 ...
- 2013 长沙网络赛J题
思路:这题对于其他能退出所有值的情况比较好像,唯一不能确定的是XXOXXOXXOXX这个形式的序列,其中XX表示未知,O表示已知. 我们令num[1]=0,那么num[4]=sum[3]-sum[2] ...
- 2013 长沙网络赛 B 题 Bizarre Routine
题解 http://blog.csdn.net/u010257508/article/details/11936129 #include <iostream> #include <c ...
- 2016 acm香港网络赛 A题. A+B Problem (FFT)
原题地址:https://open.kattis.com/problems/aplusb FFT代码参考kuangbin的博客:http://www.cnblogs.com/kuangbin/arch ...
- 2018 ACM-ICPC徐州站网络赛 G题
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xxx , yy ...
- 2015北京网络赛 G题 Boxes bfs
Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...
- 2016 acm香港网络赛 C题. Classrooms(贪心)
原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and ...
- 2016 acm香港网络赛 F题. Crazy Driver(水题)
原题网址:https://open.kattis.com/problems/driver Crazy Driver In the Linear City, there are N gates arra ...
随机推荐
- Lucene.net 多条件查询搜索
最近一直在研究lucene,目的是想让网站实现像搜索引擎那样的搜索,可以快速.准确的帮用户查询出想要的结果.废话不多说,上代码实例: 1.利用BooleanQuery进行多条件搜索(比较灵活) L ...
- pip UnicodeDecodeError: 'ascii' codec can't decode byte
http://blog.csdn.net/computerms/article/details/48056103
- jira的使用
http://www.cnblogs.com/yangxia-test/p/4448002.html linux下的安装: http://blog.csdn.net/crave_shy/article ...
- 部署samba服务之后,在客户端用挂载访问的方式,错误信息:mount: block device //192.168.1.108/mysqldata is write-protected, mounting read-only mount: cannot mount block device //192.168.1.108/mysqldata read-only
部署samba服务之后,在客户端用挂载访问的方式,错误信息:mount: block device //192.168.1.108/mysqldata is write-protected, moun ...
- 一个最小mybatis
项目结构 package hello; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis ...
- PHP memory_get_usage()管理内存
PHP memory_get_usage()管理内存 我们在实际编码中,要想实现对内存的查看和操作,许多程序员们第一个想到的就是PHP memory_get_usage()这个PHP脚本内存函数. 下 ...
- php开发环境
php一般使用xampp(apache+mysql+php+perl)部署,下载地址https://www.apachefriends.org/zh_cn/index.html.本文不用集成包,搭建P ...
- {Reship}{原文}{资治通鉴}
this article came from here ================================================= 资治通鉴 (361人评分) 9.0 作者 ...
- Math类中的BigDecimal
如果我们编译运行下面这个程序会看到什么? public class Test { public static void main(String args[]) { ...
- SQL 高效分页查询
declare @page int --页码 declare @pagecount int;--每一页显示的记录数 select * from (select *,row_number() over( ...