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 ...
随机推荐
- 一致性 hash 算法( consistent hashing )a
一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...
- SDE用户密码到期
系统使用Arcsde for oracle 11g ,一直使用正常,突然有一天客户打电话说系统中地图不显示了.登陆GIS服务器,发现sde数据已经无法链接,错误提示:“ [ORA-28002: the ...
- Hive删除数据库
DROP DATABASE是删除所有的表并删除数据库的语句.它的语法如下: DROP DATABASE StatementDROP (DATABASE|SCHEMA) [IF EXISTS] data ...
- Reactor模式通俗解释
Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应器模式,但更多好像就直接叫reactor模式了,其实我觉着叫应答者模式更好理解一些.通过了解,这个模式更像一个侍卫,一直在等待你的召唤,或 ...
- js 判断IOS版本号
先来观察 iOS 的 User-Agent 串: Phone 4.3.2 系统:Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; e ...
- java自编时间工具类
package timeTools; import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...
- 《SSM框架搭建》一.构建maven web项目
一.创建maven工程File-New-other-Maven Project 二.设置项目工作空间,取消默认勾选,手动设置 三.选择internal,输入web,选择maven.archetypes ...
- 设置TextBox控件的TextMode属性
我想在程式代碼中將TextBox控件的TextMode属性设置為Password,寫成TextBox1.TextMode=MultiLine和TextBox1.TextMode="Multi ...
- 26 Time Management(转)
01. There is alway time. Time is priorities. 时间常有.时间优先. 02. Days always fill up. 时间总会有的. Only plan f ...
- {Latex}{Tabular}文本超出表格自动换行
用p或者m可以控制每列的宽度(需载入array宏包). 要整个表格相对于页面居中,用chngpage宏包的adjustwidth,如下: \documentclass[oneside]{article ...