您的位置:首页 > 理论基础 > 计算机网络

2015 上海网络赛 ACM/ICPC Asia Regional Shanghai Online

2015-09-26 22:59 756 查看


An easy problem

题意: 两种操作 1 乘一个数 2 除一个之前乘过的数  输出答案模M

分析: 线段树 - - 每次操作更新单点 然后求区间乘积 答案就是mul[1]

代码:

//
// Created by TaoSama on 2015-09-26
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

typedef long long LL;

int n, M;
int mul[N << 2];

#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

void build(int l, int r, int rt) {
mul[rt] = 1;
if(l == r) return;
int m = l + r >> 1;
build(lson);
build(rson);
}

void update(int o, int v, int l, int r, int rt) {
if(l == r) {
mul[rt] = v % M;
return;
}
int m = l + r >> 1;
if(o <= m) update(o, v, lson);
else update(o, v, rson);
mul[rt] = 1LL * mul[rt << 1] * mul[rt << 1 | 1] % M;
}

int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

int t; scanf("%d", &t);
int kase = 0;
while(t--) {
scanf("%d%d", &n, &M);
build(root);
printf("Case #%d:\n", ++kase);
for(int i = 1; i <= n; ++i) {
int op, x; scanf("%d%d", &op, &x);
if(op == 1) update(i, x, root);
else update(x, 1, root);
printf("%d\n", mul[1]);
}
}
return 0;
}

MZ大神的思路:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
using namespace std ;
typedef long long ll ;
const int maxn = 1e5 + 5 ;
const int inf = 0x3f3f3f3f  ;
const ll mod = 1e9 + 7 ;
const double eps = 1e-6 ;
const double pi = acos(-1.0) ;

int t , n , m , kase ;
ll a[maxn] ;
int o , q ;
int p[20] , c , cnt[20] ;

void fac( int num )
{
c = 0 ;
for( int i = 2 ; i*i <= num ; i++ )
{
if( num % i == 0 )
{
p[c++] = i ;
while( num % i == 0 ) num /= i ;
}
}
if( num != 1 ) p[c++] = num ;
}

void update( int &num , int k )
{
for( int i = 0 ; i < c ; i++ )
{
while( num % p[i] == 0 )
{
num /= p[i] ;
cnt[i] += k ;
}
}
}

void show()
{
for( int i = 0 ; i < c ; i++ ) printf( " p[%d] = %d\n" , p[i] , cnt[i] ) ;
}

ll quickPow( ll a , ll b , ll c )
{
ll r = 1 ;
while( b > 0 )
{
if( b&1 ) r = r*a%m ;
b >>= 1 ;
a = a*a%m ;
}
return r ;
}

void exgcd( ll a , ll b , ll &d , ll &x , ll &y )
{
if( !b ) { d = a , x = 1 , y = 0 ; }
else
{
exgcd(b,a%b,d,y,x) ;
y -= x*(a/b) ;
}
}

ll inv( ll num )
{
ll x , y , d ;
exgcd(num,m,d,x,y) ;
return ( x%m + m ) % m ;
}

ll get( )
{
ll ret = 1 ;
for( int i = 0 ; i < c ; i++ ) ret = ret * quickPow( p[i] , cnt[i] , m ) % m ;
return ret ;
}

int main()
{
scanf( "%d" , &t ) ;
while( t-- )
{
scanf( "%d%d" , &q , &m ) ;
fac(m) ;
memset( cnt , 0 , sizeof cnt ) ;
ll ans = 1 ;
printf( "Case #%d:\n" , ++kase ) ;
for( int i = 1 ; i <= q ; i++ )
{
scanf( "%d%I64d" , &o , a+i ) ;
if( o == 1 )
{
int tp = a[i] ;
update( tp , 1 ) ;
ans = ans * tp % m ;
}
else
{
int tp = a[a[i]] ;
update( tp , -1 ) ;
ans = ans * inv(tp) % m ;
}
printf( "%I64d\n" , ans*get()%m ) ;
}
}
return 0 ;
}



Explore Track of Point

分析: http://blog.csdn.net/Baileys0530/article/details/48753151

          或者延长高线 向两腰做垂线 画圆弧

代码:

//
// Created by TaoSama on 2015-09-26
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double PI = acos(-1);

struct Point {
double x, y;
void read() {
scanf("%lf%lf", &x, &y);
}
} A, B, C, M;

double getdis(Point& a, Point& b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}

int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

int t; scanf("%d", &t);
int kase = 0;
while(t--) {
A.read(), B.read(), C.read();
M.x = (B.x + C.x) / 2, M.y = (B.y + C.y) / 2;
double a = getdis(B, M), b = getdis(A, B), h = getdis(A, M);
double r = 2 * a * h / (2 * a + 2 * b); //内接圆半径
double R = (a * a - r * r) / r / 2 + r; //
double ans = 2 * R * asin(a / R) + h;
printf("Case #%d: %.4f\n", ++kase, ans);
}
return 0;
}


Can you find it

分析: http://blog.csdn.net/queuelovestack/article/details/48754331
          快速幂的时候可以用  降幂公式降幂 - - 素数就是 (C-1)

代码:

//
// Created by TaoSama on 2015-09-26
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

typedef long long LL;

LL C, k1, b1, k2;

LL ksm(LL x, LL n, LL m) {
LL ret = 1;
while(n) {
if(n & 1) ret = ret * x % m;
x = x * x % m;
n >>= 1;
}
return ret;
}

int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

int kase = 0;
while(scanf("%d%d%d%d", &C, &k1, &b1, &k2) == 4) {
bool ok = false;
printf("Case #%d:\n", ++kase);
for(int a = 1; a < C; ++a) {
int b = C - ksm(a, (k1 + b1) % (C - 1), C);
if(ksm(a, k1 % (C - 1), C) == ksm(b, k2 % (C - 1), C)) {
ok = true;
printf("%d %d\n", a, b);
}
}
if(!ok) puts("-1");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  2015上海网络赛