HDU Math Problems
1576
const int mod = 9973;
n = a - a / mod * mod;
a / b = ans;
ans * b = a = a / mod * mod + n;
n = b * ans - a / mod * mod;
n = b * ans + mod * y;
extended_gcd(b, mod, ans, y);
#define PRON "hdu1576"
#define LL "%lld"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; const int MOD = ; int Tcase; ll extended_gcd(ll a, ll b, ll & x, ll & y){
if (b == ){
x = , y = ;
return a;
} ll d = extended_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - a / b * y; return d;
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif ll a, b, x, y; scanf("%d", &Tcase);
while (Tcase --){
scanf(LL LL, &a, &b);
extended_gcd(b, MOD, x, y);
x = ((x * a % MOD) + MOD) % MOD;
printf(LL "\n", x);
}
}
hdu1576
2824
Σphi[i]
#define PRON "hdu2824"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; const int MAXN = ; int n, m;
ll phi[MAXN + ]; void get_phi(){
memset(phi, , sizeof phi);
phi[] = ;
for (int i = ; i <= MAXN; i ++)
if (!phi[i]){
for (int j = i; j <= MAXN; j += i){
if (!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i - );
}
}
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif get_phi();
for (int i = ; i <= MAXN; i ++)
phi[i] += phi[i - ]; while (scanf("%d %d", &n, &m) == )
cout << phi[m] - phi[n - ] << endl;
}
hdu2824
1573
中国剩余定理的一般形式
#define PRON "hdu1573"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int ll; const int MAXN = + ; int Tcase, _max, n, a[MAXN], b[MAXN]; ll extended_gcd(ll a, ll b, ll & x, ll & y){
if (b == ){
x = , y = ;
return a;
} ll d = extended_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - a / b * y; return d;
} ll normal_crt(){
ll m1, m2, r1, r2, x, y; //solve N = r1 (mod m1)
// N = r2 (mod m2)
m1 = a[], r1 = b[];
for (int i = ; i < n; i ++){
m2 = a[i], r2 = b[i]; //solve d = x * m1 + y * m2
//(x, y) is the solution to the equation above
//solve c = r2 - r1 = y * m2 - x * m1
//(x0, y0) is the solution to the equation above
//x0 = x * c / d, y0 = x * c / d
ll d = extended_gcd(m1, m2, x, y);
ll c = r2 - r1;
if (c % d)
return ; ll t = m2 / d;
x = (x * c / d % t + t) % t; //r1 is the solution to the equaions from 1st to ith
r1 += m1 * x;
//m1 is the lcm of m1 to mi
m1 *= t;
} if (_max < r1)
return ; //if (x0, y0) is one of the solution
//(x0 + k * m2 / d, y0 - k * m1 / d) (k -> Z) also apply
return (_max - r1) / m1 + - (bool)(r1 == );
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif scanf("%d", &Tcase);
while (Tcase --){
scanf("%d %d", &_max, &n);
for (int i = ; i < n; i ++)
scanf("%d", a + i);
for (int i = ; i < n; i ++)
scanf("%d", b + i); printf("%d\n", normal_crt());
}
}
1573
1370
中国剩余定理
#define PRON "hdu1370"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int ll; const int MAXN = ;
const int MOD = ; int Tcase, cnt, st, a[MAXN], b[MAXN]; ll extended_gcd(ll a, ll b, ll & x, ll & y){
if (b == ){
x = , y = ;
return a;
} ll d = extended_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - a / b * y; return d;
} ll inv(ll a, ll n){
ll x, y;
ll d = extended_gcd(a, n, x, y);
return d == ? (x + n) % n : -;
} ll crt(int n){
ll ret = , m = ; for (int i = ; i < n; i ++)
a[i] %= b[i], m *= b[i]; for (int i = ; i < n; i ++)
ret = (ret + a[i] * (m / b[i]) * inv(m / b[i], b[i])) % m; ret -= st;
return ret + MOD * (bool)(ret <= );
} int main(){
#ifndef ONLINE_JUDGE
freopen(PRON ".in", "r", stdin);
#endif cnt = ;
b[] = , b[] = , b[] = ; scanf("%d", &Tcase);
while (scanf("%d %d %d %d", &a[], &a[], &a[], &st) == && !(a[] == - && a[] == - && a[] == -))
printf("Case %d: the next triple peak occurs in %d days.\n", ++ cnt, crt());
}
1370
HDU Math Problems的更多相关文章
- hdu some problems in Multi-University Training Contest
hdu 6103 Kirinriki #include<bits/stdc++.h> using namespace std; int n,m,ans; ]; void doit(int ...
- Simple Math Problems
整理下<算法笔记>,方便查看. 一.最大公约数&最小公倍数 欧几里得定理:设a,b均为正整数,那么gcd(a,b)=gcd(b,a%b). 若,定理就先交换a和b. 注意:0和任意 ...
- HDU 1220 Cube(数学,找规律)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1220 Cube Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu 1220 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1220 Cube Time Limit: 2000/1000 MS (Java/Others) Memory ...
- HDOJ/HDU 1085 Holding Bin-Laden Captive!(非母函数求解)
Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...
- HDU 1085 Holding Bin-Laden Captive! (母函数)
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- hdu 1085 Holding Bin-Laden Captive!
Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...
- HDU 1085-Holding Bin-Laden Captive!(生成功能)
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HUST 1555 A Math Homework
1555 - A Math Homework 时间限制:1秒 内存限制:128兆 338 次提交 131 次通过 题目描述 QKL is a poor and busy guy, and he ...
随机推荐
- linux 学习 14 日志管理
第十四讲 日志管理 14.1 日志管理-简介 .日志服务 在CentOS .x中日志服务已经由rsyslogd取代了原先的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服 ...
- java规范(二)
常量命名 不允许使用任何魔法值(未定义的常量)直接出现在代码中 反例: String key="Id#taobao_"+tradeId: cache.put(key, value) ...
- cf727e
题意:给你一个模式串和一堆长度相同的不相同的匹配串,问是否有一个方案可以让这个模式串由这些匹配串首尾相连组成,每个串只能出现一次. 思路:还是比较简单的,显然模式串每个位置最多匹配一个匹配串,因为所有 ...
- 史航416第11次作业&总结
作业1:冒泡排序 #include <stdio.h> ],int n); int main() { ],n,i; printf("输入一个整数n:"); scanf( ...
- Python常用模块学习
1.模块介绍 2.time & datetime模块 3.random 4.os 5.sys 6.shutil 7.json&pickle 8.shelve 9.xml处理 10.ya ...
- Apache伪静态在网站目录没有反斜杠后自动添加反斜杠
第一步:确认网站开启REWRITE规则 一般有两种情况: i.apache安装的时候已经包含rewrite功能 ii.后续配置的时候新添加mod_rewrite.so.这种情况需要在httpd.con ...
- 关于js原型继承
js的每个类都有一个prototype对象 访问对象的属性时,会先访问到对象自身是否有定义这个属性 如果没有定义,就会去访问对象所属类型的prototype对象是否有此属性 原型继承就是把类型的pro ...
- php js 排序
编写背景及排序 规则 公司需要对游戏进行一系列的排序,在这里只说我自己遇到问题的哪一段 //规则:$plat数据要根据$sort里的sort为相应 可以输入一个数字,即为该平台: 解决思路:将$so ...
- Oracle中的正则表达式
检查约束 --密码的长度必须在3-6 --年龄必须在1-120 --性别只能是男或女 --电话号码必须满足电话的格式: 手机格式,座机格式 drop table test; select * from ...
- Python SQLAlchemy --3
本文為 Python SQLAlchemy ORM 一系列教學文: 刪除 學會如何查詢之後,就能夠進行後續的刪除.更新等操作. 同樣地,以幾個範例做為學習的捷徑. 123456789 user_1 = ...