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 ...
随机推荐
- CACTI表结构和数据被动获取
cacti我们也用了很久了,但是它的表结构一直都没有去关心过,得空抽了半个晚上的时间,把它的库表结构大概看了下,某些字段的含义跟大家分享下:cacti的数据都是存放在rrdtool中的,数据库存放的其 ...
- 【WCF全析(一)】--服务协定及消息模式
上周微软开发布会说.NET支持完全跨平台和并开放Core源码的新闻,让我们顿时感到.NET要迎来它的春天.虽然早在几年前.NET就能开发Android和IOS,但是这次的跨平台把Linux都放到了微软 ...
- PVANET----Deep but Lightweight Neural Networks for Real-time Object Detection论文记录
arxiv上放出的物体检测的文章,在Pascal voc数据集上排第二.源码也已放出(https://github.com/sanghoon/pva-faster-rcnn),又可以慢慢把玩了.这篇文 ...
- jquery一些基本函数
jquery.com1.x版本兼容ie2.x版本简化适合移动端 $('li:first') 第一个$('li:eq(2)') $('li:last') 最后一个$('li:odd') 偶数行 1 3$ ...
- OC中property的有关属性
property的有关属性: (1)readwrite是可读可写特征:需要生成getter方法和setter方法: (2)readonly是只读特性只会生成getter方法不会生成setter方法: ...
- js错误:TypeError $(...) is null
转载自:http://www.phpernote.com/jquery/851.html:
- 部署IISHTTP 404.17无法由静态文件处理程序来处理
部署IIS时候出现下图问题,这是因为IIS无法处理aspx.ashx等后缀名的文件,这是因为Web 服务器接收到请求时,会对所请求的文件的文件扩展名进行检查,确定应由哪个 ISAPI 扩展处 ...
- 标准bootstrap html 页面
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- java后台如何获取String 类型 json里的字段值
首先把获取到的数据转为json String sbody=Json.getGson().toJson(resp.getResponseBody()); Huanxin 这个类是 json数据对应字段的 ...
- C# ADO.NET (sql语句连接方式)(增,删,改)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...