Introduction to Algorithmic competition 10.1 number of preliminary example code of number theory

Source: Internet
Author: User

10.1 Colossal Fibonacci numbers! UVA11582 idea: Cyclic section + fast Power
#include <cstdio> #include <cstring> #include <algorithm> #define LLU Long Long unsignedusing namespace    std;inline int Qpow (LLu x,llu y, int MOD) {x%=mod;    LLu ans = 1,tem = x;        while (y) {if (y&1) ans = (ans*tem)%mod;        TEM = (TEM * tem)%MOD;    y/=2; }return (int) ans; LLu A[1000000+5];//1 1 2 3 5 8 21inline void print (int n) {for (int i=0;i<n;i++) printf ("%llu%llu\n", A[i], A[i+1]);}    int main () {LLu A, B;    int t;scanf ("%d", &t);        while (t--) {int n;        scanf ("%llu%llu%d", &a,&b,&n); a[0]=0%n;        A[1]=1%n;int M=n*n;            for (int i=2;i<=m;i++) {a[i]= (a[i-1]+a[i-2])%n;                if (A[i]==a[1]&&a[i-1]==a[0]) {n=i-1;            Break        }}//print (n);        int ans = QPOW (a,b,n);//printf ("ans =%d\n", ans);    printf ("%llu\n", A[ans]); }}

10.3 Choose and Divide UVA10375 idea: The only number of prime numbers to decompose
#include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #define __int64 Long longusing namespace Std;const int N = 10000+5;inline __int64 C (__int64 n,__int64 m) {}int prime[2000];int Num[2000l];bool J    udge_prime[n]={0};inline int init () {int num = 0;    memset (judge_prime,false,sizeof (judge_prime));            for (int i=2;i<n;i++) {if (!judge_prime[i]) {prime[num++]=i;        for (int j=i*i;j<n;j+=i) judge_prime[j]=true; }}return num;} inline void add_primefactor (int n,int d,int primenum) {for (int. i=0;i<primenum&&n>1;i++) {while (n%prime[i]==0)            {Num[i]+=d;        N/=prime[i]; }}}inline void Add_fun (int n,int d,int primenum) {for (int i=2;i<=n;i++) add_primefactor (i,d,primenum);} void print (int n) {for (int i=0;i<n;i++) printf ("%d-->%d\n", Prime[i],num[i]), GetChar ();} inline void work (int a,int b,int c,int d,int primenum) {   Add_fun (A,1,primenum);    Add_fun (C,-1,primenum);    Add_fun (B,-1,primenum);    Add_fun (D,1,primenum);    Add_fun (A-b,-1,primenum); Add_fun (c-d,1,primenum);}    int main () {int primenum = init ();    int a,b,c,d;        while (scanf ("%d%d%d%d", &a,&b,&c,&d) ==4) {memset (num,0,sizeof (num));        Work (A,b,c,d,primenum);        Double ans = 1;            for (int i=0;i<primenum;i++) {ans *= pow (prime[i],num[i]);        if (Prime[i]>=max (a,c)) break;    } printf ("%.5f\n", ans); }}


10.4 minimun Sum LCM UVA10791 idea: the only decomposition
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std; inline int work (int a,int& x) {    int res = 1;    while (x%a==0) {        res*=a;        X/=a;    } return res;} int main () {    int n,t=0;    while (scanf ("%d", &n) ==1&&n)    {        if (n==1) {            printf ("Case%d:%d\n", ++t,2);            Continue;        }        int k=0;        Long Long ans = (long long) n+1;        int cur = 0;int M=N/2;        for (int i=2;i<=m&&i<=n;i++)        {            if (n%i==0) {                k++;                Cur + = Work (i,n);}        } printf ("cur=%d\n", cur);        if (cur) {            ans = min (ans, k<2?cur+1ll:cur+0ll);        }        printf ("Case%d:%lld\n", ++t,ans);}    }



10.5 GCD XOR UVA12716
Idea: The basic nature of XOR: A^b=c,a^c=b + sieve Prime

#include <cstdio> #include <cmath> #include <cstring> #include <algorithm>using namespace std; const int N = 30000000+5;int a[n];inline void init () {    int x = N;    memset (a,0,sizeof (A));    for (int i=1;i<=x;i++)    {        a[i]+=a[i-1];        for (int j=i*2;j<=x;j+=i)        {            int l= j^i;            if (j>l&& (j-l) ==i)                a[j]++;//printf ("a=%d,b=%d,c=%d\n", J,j^i,i);}}}    int main () {    int t,t=0;init ();    scanf ("%d", &t);    while (t--)    {        int n;        scanf ("%d", &n);        printf ("Case%d:%d\n", ++t,a[n]);}    }



Getting started with algorithmic competition 10.1 number of initial examples code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.