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 ...
随机推荐
- GCD、dispatch函数介绍
iOS多线程的方法有3种: NSThread NSOperation GCD(Grand Central Dispatch) 其中,由苹果所倡导的为多核的并行运算提出的解决方案:GCD能够访问线程池, ...
- loadrunner录制脚本如何选择使用get请求和post请求的方式
在loadrunner工具里录制脚本时常常会用到get请求和post请求,有关loadrunner常用的这两类的请求主要有: get请求: web_url 和 web_link post请求: web ...
- Salesforce 数据清洗
新系统上线后,需要导入历史数据,但是旧数据格式,数据缺失,数据错误,奇异值,属性归类与新系统有很大的gap.因此我们需要建立一套数据动态清洗规则给Salesforce系统,通过这些规则自动清洗导入数据 ...
- xampp 配置虚拟主机
1.安装好xampp后 2.找到安装目录 apache目录--conf目录--extra目录--httpd-vhosts.conf文件(用记事本或者别的编辑器打开) 文件最后添加代码 <Virt ...
- CodeSmith7代码生成器针对PostgreSQL数据库无法使用的Bug修复全过程
前言 最近公司需要将原来使用的MSSQL数据库整体迁移至pgsql,需要使用CodeSmith生成IBatisNet的配置文件.按照提示安装了Npgsql.dll后依然无法使用.引发了本次通过反编译修 ...
- scrollbar_test
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Extjs MVC开发模式详解
Extjs MVC开发模式详解 在JS的开发过程中,大规模的JS脚本难以组织和维护,这一直是困扰前端开发人员的头等问题.Extjs为了解决这种问题,在Extjs 4.x版本中引入了MVC开发模式, ...
- MySQL数据库(表)的导入导出(备份和还原)
一)在同一个数据库服务器上面进行数据表间的数据导入导出: 1. 如果表tb1和tb2的结构是完全一样的,则使用以下的命令就可以将表tb1中的数据导入到表tb2中: insert into db2.tb ...
- Android中editText使用报错
在activity_main.xml文件中添加了editText控件 <EditText android:id="@+id/edit_text" ...
- index()、e.target.value、on()与快捷处理的区别、
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...