[pe531]Chinese leftovers
题意:1e6~1e6+5000之间任意两个之间同余方程组的解。余数为欧拉函数。
解题关键:线性筛预处理,扩展中国剩余定理暴力求解。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
#define MAXN 1005002
using namespace std;
typedef long long ll;
ll x,y,r[MAXN],m[MAXN],n; //将求素数和欧拉函数值都线性解出
int prime[MAXN];//保存素数
bool vis[MAXN];//初始化
int phi[MAXN];//欧拉函数
void Lphisieve(int n){
int cnt=;
for(int i=;i<n;i++){
if(!vis[i]){
prime[cnt++]=i;
phi[i]=i-;// if p is prime,then phi[i]=i-1
}
for(int j=;j<cnt&&i*prime[j]<n;j++){
int k=i*prime[j];
vis[k]=true;
if(i%prime[j]==){
phi[k]=phi[i]*prime[j];
break;
}
else phi[k]=phi[i]*(prime[j]-);
}
}
} ll extgcd(ll a,ll b,ll &x,ll &y){
ll d=a;
if(b) d=extgcd(b,a%b,y,x),y-=a/b*x;
else x=,y=;
return d;
}
ll excrt(int n,ll *m,ll *r){
ll M=m[],pre=r[],d;//a是模数
for(int i=;i<n;i++){
d=extgcd(M,m[i],x,y);
if((pre-r[i])%d!=) return -;
x=(pre-r[i])/d*x%m[i];
pre-=x*M;
M=M/d*m[i];//lcm
pre%=M;
}
return (pre%M+M)%M;
}
int main(){
Lphisieve();
ll sum=;
for(ll i=;i<;i++){
m[]=i,r[]=phi[i];
for(ll j=i+;j<;j++){
m[]=j,r[]=phi[j];
ll t=excrt(,m,r);
if(t==-) t=;
sum+=t;
}
}
printf("%lld\n",sum);
}
[pe531]Chinese leftovers的更多相关文章
- 使用MySQL数据库将汉字转换成拼音的一个C语言小程序
环境: mysql:mysql-5.1.65 centos:centos 6.5 编译命令: gcc -o chinesetopinyin chinesetopinyin.c -L/usr/lib/m ...
- 英语阅读——Speaking Chinese in America
这篇文章是<新视野大学英语>第四册的第五单元的文章,第一遍英语阅读完后对比中文,发现自己对作者的观点理解有些出入.作者反对的是认为中国说话客套而美国人直接的观点,利用自己的经历表达了中文也 ...
- CTRL-Space always toggles Chinese IME (Windows 7、10)
一.window占用了ctrl+空格的快捷键,影响开发工具的只能提示的使用. 二.解决方式: Go to Start > Type in regedit and start it (打开运行输入 ...
- OpenCascade Chinese Text Rendering
OpenCascade Chinese Text Rendering eryar@163.com Abstract. OpenCascade uses advanced text rendering ...
- NetSuite Chinese Finance Reports
NetSuite has a strong report customization application. The standard finance reports has a different ...
- Chinese economic influence in North Korea
Where this new investment is being targeted is also interesting雄性禿 . "If you look at the econom ...
- Configure Amazon RDS mysql to store Chinese Characters
Configure Amazon RDS mysql to store Chinese Characters https://dev.mysql.com/doc/refman/5.7/en/chars ...
- Chinese culture
文房四宝 笔墨纸砚是中国古代文人书房中必备的宝贝,被称为“文房四宝”.用笔墨书写绘画在 中国可追溯到五千年前.秦(前221---前206)时已用不同硬度的毛和竹管制笔:汉代(前206—公元220) ...
- XidianOJ 1182 Chinese Paladin – Qi’s troubles
题目描述 As we all know, Xiahou Jinxuan (Chinese Paladin 5 Prequel's protagonist) and Yue Jinzhao (Chine ...
随机推荐
- python 基础 3.2 文件 for 练习
#/usr/bin/python #coding=utf-8 #@Time :2017/11/1 22:19 #@Auther :liuzhenchuan #@File :1030-1031练 ...
- Django的标准库django.contrib包介绍(转)
Django.contrib是啥? 1.它是一个强大的功能包,是Django的标准库.2.Django的标准库存放在 django.contrib 包中.每个子包都是一个独立的附加功能包. 这些子包一 ...
- formData.append("username", "Groucho"); input 文件大小
formData.append("username", "Groucho"); https://developer.mozilla.org/en-US/docs ...
- 【题解】quake
[题解]\(quake\) 题目大意 我们共有报酬\(f\)元,一条边有它的价值\(w_i\),有它的建造时间\(t_i\).要求建一些边,生成一颗树.求最大的利润率. 数据范围 \(n\le 400 ...
- subline 的常用命令
zsh 配置 编辑zsh 命令 vim .zshrc alias subl="/Applications/Sublime\ Text.app/Contents/SharedSupport/b ...
- matlab 在机器视觉中常用的函数
~ triangulate() 三角化(获得距离)匹配点 ~ undistortImage() 去除相机畸变并生成图像
- Android/iOS Remote debugging
简单介绍 使用下面方法可以定位webview中的元素,无法定位view中的元素. 原文地址:http://mp.weixin.qq.com/s/y_UfdgjT_pkKgYivJmqt7Q webvi ...
- ubuntu把文件移动到指定的文件夹
有个文件a.txt在/etc下,想把它移动到/etc/fuck文件夹中 cd /etc/fuck sudo mv a.txt 要移动到的文件夹 没报错就成功了
- jQuery的事件绑定和解除
1 . 绑定事件 语法 : bind(type,data,fn) 描述 : 为每一个匹配的特定元素(像 click)绑定一个事件处理器函数. type(String) : 事件类型 data(Obje ...
- Algorithm: dynamic programming
1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...