BZOJ4454: C Language Practice
Description

Input
Output
Sample Input
3 2
5 9 6
3 4
2 2
8 9
0 6
1 1
9
6
Sample Output
22
3
然后整个算法分为两步:
1.预处理出1到N所有数的分解式与1到sqrt(N)内两两数的gcd。
int SIZE=(int)sqrt(N);
int ans=1,d;
for(int i=0;i<3;i++) {
if(x[i]<=SIZE) d=gcd[x[i]][y%x[i]];
else if(y%x[i]==0) d=x[i];
else d=1;
ans=ans*d;y=y/d;
}
正确性显然。
全代码:
#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=1000010;
int g[1010][1010],d[maxn],pri[maxn/10],cnt;
bool vis[maxn];
int A[maxn],B[maxn],C[maxn];
void init(int n) {
rep(i,0,1005) rep(j,0,i) {
if(!i||!j) g[i][j]=i+j;
else g[i][j]=g[j][i%j];
}
rep(i,0,1005) rep(j,i+1,1005) g[i][j]=g[j][i];
d[1]=1;
rep(i,2,n) {
if(!vis[i]) pri[++cnt]=i,d[i]=i;
rep(j,1,cnt) {
if(i*pri[j]>n) break;
vis[i*pri[j]]=1;
if(i%pri[j]==0) {d[i*pri[j]]=d[i];break;}
d[i*pri[j]]=pri[j];
}
}
A[1]=B[1]=C[1]=1;
rep(i,2,n) {
int j=i/d[i];A[i]=A[j];B[i]=B[j];C[i]=C[j];
if(A[i]*d[i]<=1000) A[i]*=d[i];
else if(B[i]*d[i]<=1000) B[i]*=d[i];
else C[i]*=d[i];
}
}
int X[3];
int gcd(int x,int y) {
if(!x||!y) return x+y;
if(x<=1000&&y<=1000) return g[x][y];
int c=0;
if(A[x]!=1) X[c++]=A[x];
if(B[x]!=1) X[c++]=B[x];
if(C[x]!=1) X[c++]=C[x];
int ans=1,d;
rep(i,0,c-1) {
if(X[i]<=1000) d=g[X[i]][y%X[i]];
else if(y%X[i]==0) d=X[i];
else d=1;
ans*=d;y/=d;
}
return ans;
}
int n,m,a[2010],b[2010];
int main() {
init(1000000);
dwn(T,read(),1) {
n=read();m=read();unsigned int ans=0;
rep(i,0,n-1) a[i]=read();
rep(i,0,m-1) b[i]=read();
rep(i,0,n-1) rep(j,0,m-1) ans+=gcd(a[i],b[j])^i^j;
printf("%u\n",ans);
}
return 0;
}
BZOJ4454: C Language Practice的更多相关文章
- BZOJ 4454: C Language Practice
4454: C Language Practice Time Limit: 20 Sec Memory Limit: 24 MBSubmit: 501 Solved: 112[Submit][St ...
- 英语口语考试资料Language learning
"Learning a language is easy. Even a child can do it!" Most adults who are learning a seco ...
- 100 天从 Python 新手到大师
Python应用领域和就业形势分析 简单的说,Python是一个“优雅”.“明确”.“简单”的编程语言. 学习曲线低,非专业人士也能上手 开源系统,拥有强大的生态圈 解释型语言,完美的平台可移植性 支 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Python - 100天从新手到大师
简单的说,Python是一个“优雅”.“明确”.“简单”的编程语言. 学习曲线低,非专业人士也能上手 开源系统,拥有强大的生态圈 解释型语言,完美的平台可移植性 支持面向对象和函数式编程 能够通过调用 ...
- 国内某Python大神自创完整版,系统性学习Python
很多小伙伴纠结于这个一百天的时间,我觉得完全没有必要,也违背了我最初放这个大纲上来的初衷,我是觉得这个学习大纲还不错,自学按照这个来也能相对系统的学习知识,而不是零散细碎的知识最后无法整合,每个人的基 ...
- 课程五(Sequence Models),第二 周(Natural Language Processing & Word Embeddings) —— 0.Practice questions:Natural Language Processing & Word Embeddings
[解释] The dimension of word vectors is usually smaller than the size of the vocabulary. Most common s ...
- The 11 advantages of Java -Why you choose this language
Java is never just a language.There are lots of programming languages out there, and few of them mak ...
- Microsoft source-code annotation language (SAL) 相关
More info see: https://msdn.microsoft.com/en-us/library/hh916383.aspx Simply stated, SAL is an inexp ...
随机推荐
- makefile示例
1. 生成.so动态库 示例一: SoVer = 10010110CfgVer = 10010110 BinName = fnights.soGameName = "\"fnigh ...
- Freemarker使用入门
一.概述: FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 Template + data_model = output FreeMarker也是与Web容 ...
- tornado使用(Mac)
安装需求 Tornado 在 Python 2.5, 2.6, 2.7 中都经过了测试.要使用 Tornado 的所有功能,你需要安装 PycURL (7.18.2 或更高版本) 以及 simplej ...
- list[C++]
//双向链表 #include <iostream> using namespace std; #include <list> int main(int argc, const ...
- 4类 JavaScript 内存泄露及如何避免
原文:4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them笔记:涂鸦码龙 译者注:本文并没有逐字逐句的翻译,而是把我认为重要 ...
- JSHint配置详解
Also available on Github JSHint配置详解 增强参数(Enforcing Options) 本类参数设为true,JSHint会产生更多告警. bitwise 禁用位运算符 ...
- 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)
引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...
- cf 710E dp
题目链接: http://codeforces.com/problemset/problem/710/E 题意:要输入n个字符'a',有两种操作,一种是输入或删除一个'a',耗时x:另一种是把当前的整 ...
- 程序员必备的代码审查(Code Review)清单【转载】
在我们关于高效代码审查的博文中,我们建议使用一个检查清单.在代码审查中,检查清单是一个非常好的工具——它们保证了审查可以在你的团队中始终如一的进行.它们也是一种保证常见问题能够被发现并被解决的便利方式 ...
- maven自建仓库 Return code : 405
maven自建仓库jar包上传: jar包上传可以采用在自建仓库上系统上传以及通过配置maven setting.xml以及pom.xml上传. maven通过配置上传需要用户名密码以及maven仓库 ...