第一次讲题——数论

首先,今天是汶川地震4周年,深切的悼念!


准备了一个星期的数论,从一开始的野心勃勃,自己去做PPT,自己去选题目,自己去做解题报告。

可是没几天发现自己的精力不够用。我并不需要这些。有的只是去回顾以前学过的内容,挑选好的给大家讲,把自己的经验给大家讲,让大家快点入门。

回想去年我也是在314聆听着谢良的讲座,当时一点也听不懂,到今天能够在这里给别人讲,已是不小的进步。

昨天晚上自己模拟了这场比赛,发现很多知识都不太记得了。编程的熟练度是会下降的,但是数学的思想是不会消褪的,不正是说明还可以在翻腾一遍吗?不是原地踏步,这也是前行。知识的掌握本身就是反复的,而平时的不反复而掌握了只是自欺欺人罢了。

ACM就这样以朴素到极致的方式让你认清你自己,没有任何掩饰。里面浸透的是每一步的付出。在这样一个群体面前,我的讲解只是希望大家能够多交流。ACM的交流很重要啊,这才是ACM比赛的精华之处。通过这次讲解,我更加认识到知识体系的重要性,对大局的把握。一定要总结,就算总结不好,那也是一次自我提炼,这样一个过程才是应该去经历的。


明天就是中南的月赛,好好享受!



附:

Problem A Goldbach's Conjecture
Problem B X-factor Chains
Problem C Longge's problem
Problem D GCD & LCM Inverse
Problem E 青蛙的约会
Problem F Biorhythms
Problem G Cipher
Problem H Recurrent Function
Problem I DP?
Problem J Least Common Multiple  
http://acm.hust.edu.cn:8080/judge/contest/contest/view.action?cid=8112#overview

题目号

OJ

算法

题目大意

解法描述

2262

poj

判断素数

验证哥德巴赫猜想

从小到大枚举较小的一个数,然后判断这个数和减下来的那个数是否都为素数

3421

poj

质因数分解

给出一个数N,从1变到N,最长的长度是多少,有几种方案?

最长的长度就是质因子的个数,方案数为排列组合问题,为(t1+t2+…+tn)!/(t1!*t2!*…*tn!)

2480

poj

euler函数

 

 

2429

poj

GCD&LCM

给定GCDLCM的值,求其原值,若有多组答案,则输出和最小的一组

需要分解质因数,然后把它搞成

1061

poj

ExtendedGcd

两只青蛙按各自的速度跳,问能否在同一时间同一地点相遇?

可划归为求ax=b(modn)的形式

1006

poj

CRT


但是可能的问题是中间的结果会很大, 要当心。

1026

poj

置换群

给出一个置换,以及若干询问,对于每个询问,有kstring,求k可能很大,的个数不超过200

一开始想法是求各个循环的最小公倍数,发现会超大时间也不允许;

由于每个循环独立,故可分别求。

好的方法是先对每个元素算出k次迭代后的f(i)值,然后询问的时候O(1)就可以解决;

3708

poj

置换群+CRT

递归定义函数

F(j) = a[j] , 1<=j<d

F(n*d+j) = d*f(n)+b[j], 0<=j<d, n>=1

{A[I]}={1,2,…,d-1}

{b[i]}={0,1,…,d-1}

Fx(m)=f(f(f(…f(m)))) x times

fx(m) = k的最小非负整数x

F的递归定义是把正整数在d进制下分解

4 = (1 0 0)2 || a[1], b[0], b[0]

所以a只需d-1个元素

X的一次迭代实际上是一个置换群

上式b[0]=1

F1(4)= (1 1 1)2 = 7

所以可以预处理处一个数组a[i][k] 表示从ik最少需要几步,同时记录下每个元素的周期cycleA;b同样处理

可以得到若干个同余方程

X=a1//n1=a2//n2=…=ak//nk,kd进制下的长度

由于ni不满足两两互质,故需要用CRT的合并方程的方法;

无解的条件:

1D进制下长度不一样;

2A[i][k]不存在

3.同余方程无解;

3944

hdu

数论-综合

给定n,k,p0<=k<=n<10^9,

P<10000 is a prime)

求在杨辉三角中从(0,0)(n,k)路径上的最小和

公式容易推导为

K = min(k,n-k)

C(n+1,k)+(n-k)

接下去就是一个取模的问题了

自己整理出了一个模板 int C(int n, int k, int p);

一个错误检查了一个小时,n!(mod P)在算周期时要用while 去做,因为(1*p),(2*p)…还是有其他的因数的

1019

hdu

lcm

求给定N个数的LCM

 

 

 我自己昨天晚上写的程序,以及以前写的程序,以供参考。

Goldbach's Conjecture 

#include <cstdio>
#include <math.h>
using namespace std;
bool check(int n) {
    int m = int(sqrt(n));
    if (n == 1) return false;
    for (int i = 2; i <= m; i++)
        if (n % i == 0) return false;
    return true;
}
int main() {
    int n, i;
    while (scanf("%d", &n), n>0) {
        i = 1;
        while (true) {
            if (check(i)&&check(n-i)) break;
            i += 2;
        }
        printf("%d = %d + %d\n", n, i, n-i);
    }
    return 0;
}


B:X-factor Chains 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
//#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;
const int MAXPRIME = 1048576;


typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii>::iterator ITE_pii;
#define mp(x,y) make_pair((x), (y))
#define xx first
#define yy second

bitset<MAXPRIME+10> p;
vector<int> prime;//82100

vector<pii> n_depart;
ll fac[21];

int MakePrime(int L) {
    int i, j;
    prime.clear(); prime.push_back(2);
    p.set();p[0] = p[1] = 0; //make all to 1
    for (i = 3; i <= L; i += 2)
    if (p[i]) {
        prime.push_back(i); //printf("%d\n", i);
        if (double(i)>double(L)/i) continue;
        for (j = i * i; j <= L; j += i << 1) p[j] = 0;
    }
    return prime.size();
}


void depart(ll n, vector<pii> &a) {
    int cnt;
    a.clear();
    for (vector<int>::iterator i = prime.begin(); n > 1 && i != prime.end() && (ll)(*i)*(*i) <= n; i++)
    if (n % (*i)==0) {
        for (cnt = 0; n % (*i) == 0; n /= (*i), cnt++);
        a.push_back(mp((*i), cnt));
    }
    if (n > 1) a.push_back(mp(n, 1));
}

void Get_fac(int n) {
    fac[0] = 1;
    for (int i = 1; i <= n; i++) fac[i] = fac[i-1]*i;
}

ll n, ans;
int tot;

int main() {
    MakePrime(MAXPRIME);
    Get_fac(20);
    while (scanf("%I64d", &n) == 1) {
        depart(n, n_depart);
        ans = 1; tot = 0;
        for (ITE_pii k = n_depart.begin(); k != n_depart.end(); k++) {
            tot += k->yy;
            ans *= fac[k->yy];
        }
        ans = fac[tot]/ans;
        printf("%d %I64d\n", tot, ans);
    }
    return 0;
}

C: Longge's problem 

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <limits>
using namespace std;
typedef long long ll;

int main() {
 setbuf(stdout, NULL);
 ll N;
 ll p;
 ll a;
 ll res;
 while (cin >> N) {
  res = N;
  for (ll i = 2; i * i <= N; i++) {//这里求N得标准质因数分解很好,学会了。。
   if (N % i == 0) {
    p = i;
    a = 0;
    while (N % p == 0) {
     a++;
     N /= p;
    }
    res = res + res * a * (p - 1) / p;
   }
  }
  if (N != 1) {
   res = res * (2 * N - 1) / N;
  }
  cout << res << endl;
 }
 return 0;
}

D: GCD & LCM Inverse 

#include<ctime>
#include<iostream>
#include<fstream>
#include <cstdio>
#include <algorithm>
using namespace std;


long long factor[1000],fac_top = -1;

//计算两个数的gcd
long long gcd(long long a,long long b)
{
	if(a==0)
		return b;
	long long c;
	while(b!=0)
	{
		c=b;
		b=a%b;
		a=c;
	}
	return a;
}
//ret = (a*b)%n (n<2^62)
long long muti_mod(long long a,long long b,long long n)
{
    long long  exp = a%n, res = 0;
    while(b)
	{
        if(b&1)
		{
            res += exp;
            if(res>n) res -= n;
        }
        exp <<= 1;
        if(exp>n)
			exp -= n;

        b>>=1;
    }
    return res;
}
// ret = (a^b)%n
long long mod_exp(long long a,long long p,long long m)
{
    long long exp=a%m, res=1; //
    while(p>1)
    {
        if(p&1)//
            res=muti_mod(res,exp,m);
        exp = muti_mod(exp,exp,m);
        p>>=1;
    }
    return muti_mod(res,exp,m);
}
//miller-rabin法测试素数, time 测试次数
bool miller_rabin(long long n, long long times)
{
    if(n==2)return 1;
    if(n<2||!(n&1))return 0;

    long long a, u=n-1, x, y;
    int t=0;
    while(u%2==0){
        t++;
        u/=2;
    }
    srand(time(0));
    for(int i=0;i<times;i++)
	{
        a = rand() % (n-1) + 1;
        x = mod_exp(a, u, n);
        for(int j=0;j<t;j++)
		{
            y = muti_mod(x, x, n);
            if ( y == 1 && x != 1 && x != n-1 )
                return false; //must not
            x = y;
        }
        if( y!=1) return false;
    }
    return true;
}

long long pollard_rho(long long n,int c)
{//找出一个因子
    long long x,y,d,i = 1,k = 2;
    srand(time(0));
    x = rand()%(n-1)+1;
    y = x;
    while(true) {
        i++;
         x = (muti_mod(x,x,n) + c) % n;
        d = gcd(y-x, n);
        if(1 < d && d < n)return d;
         if( y == x)       return n;
         if(i == k) {
            y = x;
             k <<= 1;
         }
    }
}
void findFactor(long long n,int k)
{//二分找出所有质因子,存入factor
    if(n==1)return;
    if(miller_rabin(n, 10))
	{
		factor[++fac_top] = n;
		return;
	}
	long long p = n;
	while(p >= n)
		p = pollard_rho(p,k--);//k值变化,防止死循环
	findFactor(p,k);
	findFactor(n/p,k);
}


long long a[1000];
long long m,minx,ans;


int cmp(const void *a,const void *b){
	return *(long long *)a-*(long long *)b;
}

void dfs(long long s,long long num,long long t){

	if(s==m+1)
	{
		if(minx==-1||(num+t/num<minx))
		{
			minx=num+t/num;
			ans=num;
		}
		return;
	}

	dfs(s+1,num*a[s],t);
	dfs(s+1,num,t);

}

void solve(){
	qsort(factor,fac_top+1,sizeof(long long),cmp);
	a[0]=factor[0];
	long long i;
	for(i=0;i<fac_top;i++)
		if(factor[i]==factor[i+1])
		{
			a[m]*=factor[i+1];
		}
		else
		{
			m++;
			a[m]=factor[i+1];
		}
}

int main()
{
    long long s,t,n;
 //   ifstream cin("in.txt");

	while(cin>>s>>t)
	{
		n=t/s;
		if(s==t)
		{
			cout<<s<<' '<<t<<endl;
			continue;
		}
		minx=-1;
		m=0;
        fac_top = -1;
        findFactor(n,107);
		solve();
		dfs(0,1,n);
		if(ans>n/ans) ans=n/ans;
		cout<<s*ans<<' '<<s*(n/ans)<<endl;
    }
    return 0;
}
//http://www.cnblogs.com/zhaozhe/archive/2011/04/12/2013979.html

E: 青蛙的约会 

#include <stdio.h>
	long long exgcd(long long a, long long b, long long &x, long long &y) {
		if (b == 0) {
			x = 1; y = 0; return a;
		}
		long long g = exgcd(b, a % b, x, y);
		long long t = x - (a / b) * y;
		x = y;
		y = t;
		return g;
	}
int main() {
	long long x, y, m, n, l, x0, y0, a, b, c, d, t;
	scanf("%I64d%I64d%I64d%I64d%I64d", &x, &y, &m, &n, &l);
	a = n-m; b = l; c = x-y;
	if (a < 0) { a = -a; c = -c; }
	long long g = exgcd(a, b, x0, y0);
	if (n == m || (x - y) % g != 0) printf("Impossible\n"); else {
		b /= g;
		c /= g;
		t = c * x0;
		printf("%I64d\n", (t % b+b) % b);
	}
	return 0;
}


F:Biorhythms 

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 3;
LL n[maxn] = {23,28,33};
LL a[maxn], m[maxn], mi[maxn];
LL N, pi;

//ax+by=gcd(a,b)
LL exgcd(LL a, LL b, LL &x, LL &y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    };
    LL g = exgcd(b, a%b, x, y);
    LL t = x-a/b*y;
    x = y;
    y = t;
    return g;
}
//ax=1(mod n), if gcd(a,n)!=1 inverse do not exist
LL inverse(LL a, LL n) {
    LL d, x, y;
    d = exgcd(a, n, x, y);
    if (d != 1) return -1;
    x = (x%n+n)%n;
    return x;
}


LL cases, d, ans;

int main() {
    N = 3;
    cases = 0;
    while (true) {
        pi = 1;
        for (int i = 0; i < N; i++)    {
            scanf("%lld", &a[i]);
if (a[i]==-1) return 0;
            pi = pi*n[i];
        }
        ans = 0;
        for (int i = 0; i < N; i++) {
            m[i] = pi/n[i];
            mi[i] = inverse(m[i], n[i]);
            ans = (ans+a[i]*m[i]*mi[i]%pi)%pi;
        }
        scanf("%lld", &d);
        ans = (ans-d+pi-1)%pi+1;
        printf("Case %lld: the next triple peak occurs in %lld days.\n", ++cases, ans);
    }
}


G:Cipher 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 210;
int n, next[maxn], k;
int d[maxn][maxn], ds[maxn];
char str[maxn], ans[maxn];

int i, j;
int main() {
	while (scanf("%d", &n), n>0) {
		for (i = 0; i < n; i++) scanf("%d", &next[i]);
		for (i = 0; i < n; i++) next[i]--;
		for (i = 0; i < n; i++) {
			d[i][0] = i; ds[i] = 1;
			for (j = next[i]; j != i; j = next[j])
				d[i][ds[i]++] = j;
		}
		while (scanf("%d", &k), k>0) {
			getchar();
			gets(str);
			i = 0; while (str[i]) i++;  while (i<n) str[i++]=' ';
			for (i = 0; i < n; i++) ans[d[i][k%ds[i]]] = str[i];
			ans[n] = 0;
			puts(ans);
		}
		printf("\n");
	}
	return 0;
}


H:Recurrent Function 

/*
 * Author: Zhang Jiangbin 
 * Created Time:  2011/8/20/星期六 12:52:34
 * File Name: a.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
#define SZ(v) ((int)(v).size())
typedef __int64 LL;
const int maxd = 105;


const int maxn = 405;
LL MODa[maxn], MODn[maxn];
LL MODN;

LL exgcd(LL a, LL b, LL &x, LL &y);
LL inverse(LL a, LL n);
LL ChineseRemainderTheorem(LL N, LL a[], LL n[]);

void prepare();
void read(int c[], int&);
int divide(int &L, int c[], int d);

int a[maxd], b[maxd];
int d, i, j;
int A[maxd][maxd], B[maxd][maxd], cycleA[maxd], cycleB[maxd];
char st[maxd];
int nu[maxd];
int c1[maxn], c2[maxn], len1, len2;//lg2(10^100)<400
bool ok;
int t;
LL ans;
int main() 
{          
    //freopen("a.in", "r", stdin);
    while (scanf("%d", &d), d>=0) {
        for (i = 1; i < d; i++)
            scanf("%d", a+i);
        for (i = 0; i < d; i++)
            scanf("%d", b+i);
        prepare();
        read(c1, len1);
        read(c2, len2);
        if (len1!=len2) 
            puts("NO"); 
        else {
            ans = 1;
            ok = true;
            MODN = 0;
            for (i = 0; i < len1-1; i++){
                t = B[c1[i]][c2[i]];
                if (t==-1) {
                    ok = false;
                    break;
                }
                MODa[MODN] = t; MODn[MODN] = cycleB[c1[i]];
                MODN++;
            }
            t = A[c1[i]][c2[i]];
            if (t == -1){
                ok = false;
            } else {
                MODa[MODN] = t; MODn[MODN] = cycleA[c1[i]];
                MODN++;
            }
            if (ok) {
                ans = ChineseRemainderTheorem(MODN, MODa, MODn);
                if (ans==-1) ok = false;
            }
            if (!ok) puts("NO"); else printf("%I64d\n", ans);
        }
    }
    return 0;
}
void prepare(){
    int i, j, t;
    memset(A,-1,sizeof(A));
    memset(B,-1,sizeof(B));
    for (i = 1; i < d; i++){
        A[i][i] = 0;
        t = i; //printf("t:%d ", t);
        for (j = 1; j <= d; j++){
            t = a[t];//printf("t:%d ", t);
            if (t == i) {
              cycleA[i] = j;
              break;   
            }
            A[i][t] = j;
        }
  //      printf("A : %d : cycle : %d\n", i, cycleA[i]);
    }
    for (i = 0; i < d; i++){
        B[i][i] = 0;
        t = i;//printf("t:%d ", t);
        for (j = 1; j <= d; j++){
            t = b[t];//printf("t:%d ", t);
            if (t == i){
                cycleB[i] = j;
                break;
            }
            B[i][t] = j;
        }
//        printf("B : %d : cycle : %d\n", i, cycleB[i]);
    }
    //debug
}
void read(int c[], int &len) {
    int l, num, i, j;
    
    scanf("%s", st);
//    printf("%s\n", st);
    l = strlen(st);
    memset(nu,0,sizeof(nu));
    for (i = 0, j = l-1; i < l; i++, j--)
        nu[i] = st[j]-'0';
    len = 0;
    do{
        c[len++] = divide(l, nu, d);
//        printf("%d ", c[len-1]);
    } while(l);  
//    printf("\n");
}
int divide(int &l, int c[], int d) {
    int num, i;
    num = 0;
    for (i = l-1; i >= 0; i--){
        num = num*10+c[i];
        c[i] = num/d;
        num %= d;
    }
    while (l&&(!c[l-1])) l--;
    return num;
}
//ax+by=gcd(a,b)
LL exgcd(LL a, LL b, LL &x, LL &y) {
	if (b == 0) {
		x = 1; y = 0;
		return a;
	};
	LL g = exgcd(b, a%b, x, y);
	LL t = x-a/b*y;
	x = y;
	y = t;
	return g;
}
//ax=1(mod n), if gcd(a,n)!=1 inverse do not exist
LL inverse(LL a, LL n) {
	LL d, x, y;
	d = exgcd(a, n, x, y);
	if (d != 1) return -1;
	x = (x%n+n)%n;
	return x;
}
//方程的个数N, 形式为 x=a(i)(mod n(i))
//无解返回-1 
LL ChineseRemainderTheorem(LL N, LL a[], LL n[]) {
	LL d, x, y, c;
	for (LL i = 0; i < N; i++)
		if (a[i] >= n[i]) return -1;
	for (LL i = 1; i < N; i++) {
		//n[i-1]*x=(a[i]-a[i-1]) (mod n[i]) 
		d = exgcd(n[i-1], n[i], x, y);
		if ((a[i]-a[i-1])%d!=0) return -1;
		x = x*((a[i]-a[i-1])/d);
		x = (x%n[i]+n[i])%n[i];//non-negative
		x = x%(n[i]/d);//should get the minest x, actually it needn't for it doesn't matter a[i]
		//calculate new a and n
		c = a[i-1]+x*n[i-1];
		//c = a[i-1]+(x+i*n[i]/d)*n[i-1]=a[i-1]+x*n[i-1]+i*(n'[i]);
		n[i] = n[i]/d*n[i-1];
		a[i] = c%n[i];
	}
	return a[N-1];
}


I: DP? 

/*
 * Author: Zhang Jiangbin 
 * Created Time:  2011/8/16/星期二 12:48:42
 * File Name: e.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define SZ(v) ((int)(v).size())
const int maxp = 10005;
#define maxprime 10001//9973
#define primes 1240//1229


int f[primes][maxp], inv[primes][maxp];

int n, k ,p;

int makeprime(int L); 
void prepare();
int exgcd(int a, int b, int &x, int &y); 
int inverse(int a, int n);//求a对于质数n的逆元 
int get(int n, int p);//求n!中质因子p的个数 
int quick(int a, int b, int p);//乘法快速幂 


bool ph[maxprime+10];//1 is a prime;0 is not
int prime[primes], primelen;//prime[i] is the i-th prime, 0..primelen-1


int getf(int pos, int n) {
    int t1 = quick(f[pos][p-1],n/p, p);
    if (n%p) t1 = (t1*f[pos][n%p])%p;
    return t1;
}

int getinv(int pos, int n) {
    int t1 = quick(inv[pos][p-1],n/p, p);
    if (n%p) t1 = (t1*inv[pos][n%p])%p;
    return t1;
}
int C(int n, int k, int p) {
    int numofp = get(n, p)-get(k, p)-get(n-k, p);
    if (numofp>0) return 0;
    
    //binary search
    int l = 0, r = primelen;
    while (l+1 < r) {
        int mid = (l+r)/2;
        if (p>=prime[mid]) l = mid; else r = mid;
    }
    int pos = l;
//    printf("%d %d\n", prime[pos], p);
    
    int ans = 1, nn;
    
    nn = n;
    while (nn>0) {
          ans = (ans*getf(pos,nn))%p;
          nn/=p;
    }
    nn = k;
    while (nn>0) {
          ans = (ans*getinv(pos,nn))%p;
          nn/=p;
    }
    nn = n-k;
    while (nn>0) {
          ans = (ans*getinv(pos, nn))%p;
          nn/=p;
    }

    return ans;
}

int main() 
{   
    prepare();
    int cases = 0;  
    while (scanf("%d%d%d", &n, &k, &p) == 3) {
        k = max(k, n-k);
        int ans = C(n+1, k+1, p);
//        printf("%d\n", ans);
        ans += k;
        ans = (ans%p+p)%p;
        printf("Case #%d: %d\n", ++cases, ans);
    }
    return 0;
}














/*求逆元*/ 
//ax+by=gcd(a,b)
int exgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    };
    int g = exgcd(b, a%b, x, y);
    int t = x-a/b*y;
    x = y;
    y = t;
    return g;
}
//ax=1(mod n), if gcd(a,n)!=1 inverse do not exist
int inverse(int a, int n) {
    int d, x, y;
    d = exgcd(a, n, x, y);
    if (d != 1) return -1;
    x = (x%n+n)%n;
    return x;
}

//构造小于等于L的素数表
//ph[i]=1为素数 ph[i]=0不是素数,注意1
//prime[i],0..primelen-1为素数表 
int makeprime(int L) {
    memset(ph,1,sizeof(ph));
    primelen = 0;
    ph[1] = 0;
    for (int i = 2; i <= L; i++)
    if (ph[i]) {
        prime[primelen++] = i;
        //printf("%d\n", i);
        for (int j = i+i; j <= L; j += i) ph[j] = 0;
    }
    return primelen;
}
void prepare() {
    makeprime(10000);
//    printf("%d\n", prime[primelen-1]);
    for (int k = 0; k < primelen; k++) { 
        int p = prime[k];
        f[k][0] = f[k][1] = 1;
        inv[k][0] = inv[k][1] = 1;
        for (int i = 2; i < p; i++) {
            f[k][i] = (f[k][i-1]*i)%p;
            inv[k][i] = (inv[k][i-1]*inverse(i,p))%p;
        }
//        printf("%d %d\n", p, f[k][p-1]);
    }
}



//cnt = n! 个p
int get(int n, int p) {
    int cnt = 0;
    while (n>0) {
        cnt += n/p;
        n/=p;
    }
    return cnt;
}
int quick(int a, int b, int p) {
    int ans = 1, tmp = a%p;
    while (b>0) {
        if (b & 1) ans = (ans*tmp)%p;
        b/=2;
        tmp = (tmp*tmp)%p;
    }
    return ans;
}



J:Least Common Multiple 

#include <iostream>
using namespace std;
int gcd(int a, int b) {
    if (b == 0) return a; else return gcd(b, a % b);
}
int LCM(int a, int b) {
    int c = gcd(a, b);
    return a / c * b;
}
int c, n, lcm;
int a[1000];
int main(){
    scanf("%d", &c);
    while (c--) {
      scanf("%d", &n);
      for (int i = 1; i <= n; i++) 
          scanf("%d", &a[i]);
      int lcm = a[1];
      for (int i = 2; i <= n; i++)
          lcm = LCM(a[i], lcm);
      printf("%d\n", lcm);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值