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 ...
随机推荐
- ASP.NET泛型List的各种用法Skip、Take等
List在.NET里面使用得非常频繁,但有好多人不了解它各种小用法.我就一直记不大住... asp.net中List的简单用法,例如: 1 2 3 4 5 6 7 List<int> li ...
- EF 底层基础方法
1 using System; 2 using System.Data; 3 using System.Collections.Generic; 4 using System.Data.Entity; ...
- 9.3 js基础总结3
2.后增量/后减量运算符 ++,-- var i = 10; var a = i++; // i = i + 1; alert(a); 3.比较运算符(>,<,>=,<=,== ...
- Android客户端稳定性测试——Monkey
修改时间 修改内容 修改人 2016.6.20 创建 刘永志 2016.6.29 完成 刘永志 Monkey简介: Android SDK自带的命令行测试工具,向设备发送伪随机事件流,对应用程序进行进 ...
- FreeMaker实现变量求和
今天在项目上遇到统计分页页面的某个字段的总和,前台页面是使用FreeMaker实现的,记录一下: <#assign tprice = 0 > <#list orderlist ...
- javascript DOM 操作
在javascript中,经常会需要操作DOM操作,在此记录一下学习到DOM操作的知识. 一.JavaScript DOM 操作 1.1.DOM概念 DOM :Document Object Mode ...
- CentOS 6.5 下离线安装nginx
一.由于linux下安装nginx 需要 zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc这些依赖,而这些依赖打安装包在我们系统的光 ...
- SQL中EXISTS怎么用[转]
SQL中EXISTS怎么用 1 2 3 4 分步阅读 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False 方法/步骤 1 EXISTS用于 ...
- flash中htmlText样式
赋值htmlText时,htmlText中指定了样式的按指定样式显示,没有指定的按该文本的默认样式显示. PS:flash软件拖出来的文本,在赋值htmlText的时候该文本原有样式会失效,而new出 ...
- android studio中断开SVN连接,并彻底清理项目中的.svn文件
首先,断开SVN连接: 在使用SVN过程中,我们会发现当我们第一次share到subversion的时候,下次就无法重新share了,也无法断开连接,就算我们将工程目录下的.svn目录删除它还是会无法 ...