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 ...
随机推荐
- MySQL 第二篇
一.MySQL多实例介绍 mysql多实例,共用一套mysql安装程序,使用不同的配置文件(my.cnf).启动程序.和数据文件,即在一台服务器上同时开启多个不同的服务器端口(3306,3307),同 ...
- WebServers发布提示oracle客户端模式不一致
问题:System.InvalidOperationException: 尝试加载 Oracle 客户端库时引发BadImageFormatException.如果在安装32 位Oracle 客户端组 ...
- jquery template模版引擎
jTemplates http://jtemplates.tpython.com/ jquery-template https://github.com/codepb/jquery-template ...
- python之路-Day6
time & datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) # ...
- CentOS6.5中配置Rabbitmq3.6.6集群方案
一.安装Erlang环境 1.下载erlang源代码 wget http://erlang.org/download/otp_src_19.1.tar.gz 2.安装依赖相关库文件 yum insta ...
- Linux QT
Check system version 1. cat /proc/version mike@sp-ThinkPad-X220:~$ cat /proc/versionLinux version 3 ...
- Android Runtime
[Android Runtime] Every Android application runs in its own process, with its own instance of the Da ...
- Java 零基础之作业小练习
[练习1] 需求:输入学员的名称及总科目数并显示每项科目成绩的分数,算出总成绩. package demo2; import java.util.Scanner; //先import Scanner语 ...
- git代理设置方法解决
git config --global https.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0. ...
- CSS 图片倾斜的制作
<style> #zhong{ height:600px; width:1350px; position:relative; z-index:2} .znei{ height:60px; ...