1. 程式人生 > >HDU 3388 與m,n 互質的第k個整數

HDU 3388 與m,n 互質的第k個整數

HDU 3388
題意:求與m, n 互質的第k 個整數。

思路:

AC程式碼:

#include <stdio.h>  
#include <string.h>  

const int N = 1005;  
int phi[N * 10], vis[N * 10], m, n, a[N];  
char M[15];  

int pow_mod(int a,int n, int m)
{
    if(n == 0) return 1;
    int x = pow_mod(a,n>>1,m);
    int ans = x*x%m;
    if
(n&1) ans = ans*a%m; return ans; } int dfs(int i, int mod) { if (i == n - 1) { if (a[i] >= mod) return a[i] % mod + mod; return a[i]; } int k = dfs(i + 1, phi[mod]); return pow_mod(a[i], k, mod); } int main() { for (int
i = 1; i <= 10000; i++) phi[i] = i; for (int i = 2; i <= 10000; i++) { if (vis[i]) continue; for (int j = i; j <= 10000; j += i) { phi[j] = phi[j] / i * (i - 1); vis[j] = 1; } } int cas = 0; while (~scanf("%s", M) && M[0
] != '#') { sscanf(M, "%d", &m); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); printf("Case #%d: %d\n", ++cas, dfs(0, m) % m); } return 0; }

相關推薦

HDU 3388 mn k整數

HDU 3388 題意:求與m, n 互質的第k 個整數。 思路: AC程式碼: #include <stdio.h> #include <string.h> const int N = 1005; int phi

hdu 4135 a到b的範圍中多少數n(容斥)

namespace rim 所有 += ont put contain 質因數 tor Co-prime 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input

N(10^14)以內N的數的和(容斥原理或者尤拉函式)

#include <iostream> #include <cstring> #include <algorithm> #include <cmath>

HDU 5514(Frogs-n的數的求和)

給一個數列ai|m(1≤m≤109), 對<m且為至少一個ai的倍數的數,求和 先把重複的數,倍數關係的數去掉。 顯然m的因子不超過200個,令D為m的因子集合 此時ai均為m的因子, 賽場上可以O(2n) 暴力 但hdu上不行 考慮O(|

hdu3501 給出一個N求1..NN的數的和

給出一個N,求1..N中與N互質的數的和 ifgcd(n,i)=1 then gcd(n,n-i)=1 (1<=i<=n) 反證法:         如果存在K!=1使gcd(n,n-i)=k,那麼(n-i)%k==0         而n%k=0       

Rabbit Kingdom(hdu 4777 求區間內所有數都的數的個數 樹狀陣列)

題目連結: hdu 4777 Rabbit Kingdom 牛客: 小樂樂學數學 hdu英文題面,牛客中文題面,一個意思。   題意: 有一個長度為 n 的序列,m 個詢問。每個詢問求區間內與所有數都互質的數的個數。   思路: 對於序列

求1到nn的和(數論)解釋及證明

給出一個N,求1…N中與N互質的數的和 sigma (i=1…n) i*[gcd(i,n)==1] 反證法:gcd(n,i)=1 如果存在K!=1使gcd(n,n-i)=k,那麼(n-i)%k==0且n%k=0 那麼必須保證i%k=0。 i%k == 0 &&am

【轉AekdyCoin】求小於等於NN的數的和

話說我以前求這樣的問題都是先求與N不互質的數,把N分解質因數,然後用容斥原理,今天看了大牛的部落格,頓時覺得弱爆了。。。 以下內容轉大牛文章: if gcd(n,i)=1 then gcd(n,n-i)=1 (1<=i<=n) 反證法: 如果存在K!=1使gcd

【容斥原理-求區間內n的數】HDOJ Co-prime 4135

Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-

UVA 12493 Stars (尤拉函式--求1~nn的個數)

大致題意:圓上有偶數n個點,每m個點連起來,最後可以把所有點串聯起來就合法。問有多少個m可以完成串聯,串聯後形狀相同的算重複 n <2^31 思路:可以寫個暴力程式,可以發現只要m與n互質,就可以完成串聯,所以用尤拉函式解決 證明: 設cnt為當第一次達到原點時

尤拉函式(提供1到NN的數)

當個板子放著,具體是看了這篇部落格:尤拉函式求法與應用 尤拉函式用希臘字母φ表示,φ(N)表示N的尤拉函式. 對φ(N)的值,我們可以通俗地理解為小於N且與N互質的數的個數(包含1). //直接求解尤拉函式 int euler(int n){ //返回euler(n

小於n的數中n的數的和

題意: 給一個n,求 少於或等於n的數中與n不互質的數的和 我們先求 少於或等於n的數中與n互質的數的和 對於i與n互素 gcd(n,i)=1 必有gcd(n,n-i)=1 設n的尤拉函式值為f[n] 則有f[n]個數與n互素,這些數兩兩相加必等於n

尤拉函式(求n的數的個數)

求解與n(1-n-1)互質的質因子的個數 解析:(轉) 定義:對於正整數n,φ(n)是小於或等於n的正整數中,與n互質的數的數目。     例如:φ(8)=4,因為1,3,5,7均和8互質。 性質:1.若p是質數,φ(p)= p-1. 2.若n是質數p的k

求小於nn的數的和

給出一個N,求1..N中與N互質的數的和 ifgcd(n,i)=1 then gcd(n,n-i)=1 (1<=i<=n) 反證法:         如果存在K!=1使gcd(n,n-i)=k,那麼(n-i)%k==0         而n%k=0

【尤拉函式】(小於或等於n的數中n的數的數目)

【尤拉函式】 在數論,對正整數n,尤拉函式是少於或等於n的數中與n互質的數的數目。此函式以其首名研究者尤拉命名,它又稱為Euler's totient function、φ函式、尤拉商數等

HDU 5288(OO’s Sequence-區間情況統計)

next left res span gree sea height mission function OO’s Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/1

hdu 1930 中國剩余定理(模板題)

into name multiple pro while hat ons orm cati And Now, a Remainder from Our Sponsor Time Limit : 5000/1000ms (Java/Other) Memory Limit

中過象棋棋盤一半 馬踏日 從(1,1)出發只能向右踏出到(mn)有多少路徑

# include<stdio.h> # include<math.h> # define M 9 # define N 5 int a[10]={1},b[10]={1},sum=0,m,p;//a[]儲存橫座標,b[]儲存縱座標 int check(int n){ i

迴圈連結串列報數問題,n個人編號分別為1,2,3,……nk編號開始數1,2到m然後刪除m個人然後下 一個人再從1開始數數到m然後再刪除m

#include<iostream> #include<stdio.h> using namespace std; //迴圈連結串列報數問題,n個人編號分別為1,2,3,……n,從第k個編號開始數1,2到m個然後刪除第m個人,然後下 //一個人再從1

jsonlistmap轉 ,Json語法

Json 定義Json語法Json使用JSON 解析器Json與常見的型別之間的轉換Json-jar包下載連結定義        JSON(JavaScript Object Notation, JS 物件標記) 是一種輕量級的資料交換格式。       它基於 ECMASc