HDU 6211 卡常数取模 预处理 数论
求所有不超过1e9的 primitive Pythagorean triple中第2大的数取模$2^k$作为下标,对应a[i]数组的和。
先上WIKI:https://en.wikipedia.org/wiki/Pythagorean_triple
里面有通过欧几里得公式来得到有关毕达哥拉斯式子的一些性质。


最后得到的一个关于互质的m,n变种的式子更加直观,因此枚举m,n,保证其合法。每次枚举n,筛掉和n有共同因子的m,范围是$\sqrt{1e9}$。然后由于要求的是b,而且取模的都是2的幂指,因此可以先预处理所有$b%(2^{17})$的个数,最后再模$(2^k) $即可。
但是用这个方法做这道题还会卡取模的常数,需要把取模换成$\&(2^{17} - 1)$
另一提,半夜两点和偶像在做同这题,人家是1A秒杀,而且代码很短,还是蛮高兴的,并不(。
/** @Date : 2017-09-17 23:49:14
* @FileName: HDU 6211 青岛网络1006 欧几里得 数论.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 32000;
const double eps = 1e-8; int pri[N];
bool vis[N];
int c = 0;
int prime()
{
MMF(vis);
for(int i = 2; i < N; i++)
{
if(!vis[i]) pri[c++] = i;
for(int j = 0; j < c && i * pri[j] < N; j++)
{
vis[i * pri[j]] = 1;
if(i % pri[j] == 0) break;
}
}
}
LL coe[(1 << 17) + 20];
LL s[(1 << 17) + 20];
LL MA = (1LL<<17);
int main()
{
int T;
MMF(coe);
prime();
//cout << c << endl;
for(int i = 1; i * i <= 1000000000; i++)
{
MMF(vis);
int t = i;
for(int j = 0; j < c && pri[j] * pri[j] <= t; j++)
{
if(t % pri[j] == 0)
{
while(t % pri[j] == 0)
t /= pri[j];
for(int k = pri[j]; k <= N; k+=pri[j])
if(!vis[k]) vis[k] = 1;
}
}
if(t > 1)
for(int k = t; k < N; k+=t)
if(!vis[k]) vis[k] = 1;
for(int j = i + 1; j * j + i * i <= 1000000000; j++)
{
if(!vis[j]/*__gcd(i, j) == 1*/ && !(j&1 && i&1))
{
int a = 2 * i * j;
int b = j * j - i * i;
coe[max(a, b) & (MA - 1)]++;
//ans += s[max(a, b) % MA];
}
} //cout << ans << endl;
}
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
LL ans = 0;
int ma = (1 << n);
for(int i = 0; i < ma; i++)
scanf("%lld", s + i);
for(int i = 0; i < MA; i++)
ans += (LL)s[i & (ma - 1)] * coe[i];
printf("%lld\n", ans);
}
return 0;
}
/*
5
2
0 0 0 1
2
1 0 0 0
2
1 1 1 1
*/
//https://en.wikipedia.org/wiki/Pythagorean_triple
HDU 6211 卡常数取模 预处理 数论的更多相关文章
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- HDU 4632 区间DP 取模
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...
- HDU 5698 大组合数取模(逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- ACM卡常数(各种玄学优化)
首先声明,本博文部分内容仅仅适用于ACM竞赛,并不适用于NOIP与OI竞赛,违规使用可能会遭竞赛处理,请慎重使用!遭遇任何情况都与本人无关哈=7= 我也不想搞得那么严肃的,但真的有些函数在NOIP与O ...
- 【libreOJ模板】并查集(输入挂,取模与find优化)
1.了解了各种输入挂性orz,找到了一个合适的 2.find用while写能快一倍,并且能被数据卡掉 3.取模只能快十几毫秒,但也能被数据卡掉 取模find双优化是1997mm过的 再加一个性价比较高 ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
随机推荐
- PyCharm如何设置源代码字体的大小
改源代码大小 1.File→Settings→Editor→Colors&Fonts→Font 2.首先得需要Save as一个Scheme,接下来才可以修改字体,名字可以任意取 改运行字体的 ...
- UVA 10328 - Coin Toss dp+大数
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- 继承&成员函数&覆盖
//成员函数. /* 当子父类中出现成员函数一模一样的情况,会运行子类的函数. 这种现象,称为覆盖操作.这时函数在子父类中的特性. 函数两个特性: 1,重载.同一个类中.overload 2,覆盖.子 ...
- 开发环境解决 kafka Failed to send messages after 3 tries
新建了一个kafka集群,在window下写了一个简单的producer做测试,结果遇到了消息发送失败的问题,代码如下: Properties props = new Properties(); pr ...
- Hibernate(八)
三套查询之Criteria查询 完全面向对象的,不需要写任可查询语句. 1.查询所有的学生 //1.查询所有的学生 @Test public void test1(){ Criteria criter ...
- PAT 甲级 1050 String Subtraction
https://pintia.cn/problem-sets/994805342720868352/problems/994805429018673152 Given two strings S~1~ ...
- PHP 在windows下配置sendmail,通过 mail() 函数发送邮件
php mail()函数在windows不能用,需要安装sendmail. 1. 从http://glob.com.au下载sendmail.zip 2. 解压sendmail.zip到目录下(最好使 ...
- Hibernate 中 load() 方法导致的 noSession 异常
之所以要写这个,是因为最近碰到了一个延迟加载的 load() 导致出现 noSession 的异常. 下面第三种方式解决这个问题需要用到一个本地线程的对象,也就是 ThreadLocal 类,之前写过 ...
- 在64位系统上部署BDE的要点
首先,据我所知,Borland/CodeGear没有发布过支持64bit windows的BDE安装包,如果你在网上看到了相关的BDE安装包,很有可能是使用者自己重新打包发布的. 无论是在32bit ...
- 在DbGrid中,不按下Ctrl,单击鼠标如何实现多选?谢谢
解决方案 » 有了dbgrid1.options.dgmultiselect:=true;必须按下Ctrl键,才能实现多选, 修改源代码,把以下内容if Select and (ssShift i ...