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 ...
随机推荐
- 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))
sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个有向图 ...
- react.js 多个组件集成示例
这个看得有点懵, 可能要结合其它实例看. html <!DOCTYPE html> <html> <head> <script src="http: ...
- at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针
原来发布到weblogic 的项目,想改动发布到tomcat上.启动发布一切都正常.出入项目请求路径却包错: java.lang.NullPointerException at org.apache. ...
- Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏
效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- wp8 入门到精通 数据库更新字段(一)
public class UserInfoDB : BaseDB { public UserInfoDB() : base(@"Data Source=isostore:\MakeLove\ ...
- CI框架获取post和get参数 CodeIgniter
请参考:CI文档的输入类部分: $this->input->post() $this->input->get() ------------------------------- ...
- hdu 4753 2013南京赛区网络赛 记忆化搜索 ****
看到范围基本可以想到dp了,处理起来有点麻烦 #include<iostream> #include<cstdio> #include<cstring> #incl ...
- 阿里云(ECS)Centos服务器LNMP环境搭建
阿里云( ECS ) Centos7 服务器 LNMP 环境搭建 前言 第一次接触阿里云是大四的时候,当时在校外公司做兼职,关于智能家居项目的,话说当时俺就只有一个月左右的 php 后台开发经验(还是 ...
- Android的图片缓存ImageCache(转)
为什么要做缓存? 在UI界面加载一张图片时很简单,然而如果需要加载多张较大的图像,事情就会变得更加复杂.在许多情况下(如ListView.GridView或ViewPager等的组件),屏 ...
- Codeforces Beta Round #95 (Div. 2) D.Subway
题目链接:http://codeforces.com/problemset/problem/131/D 思路: 题目的意思是说给定一个无向图,求图中的顶点到环上顶点的最短距离(有且仅有一个环,并且环上 ...