洛谷 2158 數論 打表 尤拉函式

2022-04-07 04:37:40 字數 2415 閱讀 9233

#洛谷 2158 數論 遞推 尤拉函式 打表找規律

傳送門 (

其實看到sdoi就有一種不太好的預感,,想當年那個豬國殺,,,呵呵,,

用二維陣列維護每個點能否被選擇,之後二維列舉每個點,如果沒有被選擇就選擇,並且用它去遮擋其他所有點

#include #include #include const int maxn = 1000 + 10;

int a[maxn][maxn];

int n;

int sum;

// int ansx[maxn];

// int ansy[maxn];

void update (int x, int y)

}int main ()

sum = 2;

// ansx[1] = 1;

// ansy[1] = 2;

// ansx[2] = 2;

// ansy[2] = 1;

for (int i = 2; i <= n; i++) }}

printf("%d\n", sum);

//for (int i = 1; i <= sum; i++)

return 0;

}

其實這道題的正解是打表打出來的,大概把數字取到10左右就能發現某種性質,

x : 1  y : 2

x : 2 y : 1

x : 2 y : 2

x : 2 y : 3

x : 2 y : 4

x : 2 y : 5

x : 2 y : 6

x : 2 y : 7

x : 2 y : 8

x : 2 y : 9

x : 2 y : 10

x : 3 y : 2

x : 3 y : 4

x : 3 y : 6

x : 3 y : 8

x : 3 y : 10

x : 4 y : 2

x : 4 y : 3

x : 4 y : 5

x : 4 y : 6

x : 4 y : 8

x : 4 y : 9

x : 5 y : 2

x : 5 y : 4

x : 5 y : 6

x : 5 y : 8

x : 5 y : 10

x : 6 y : 2

x : 6 y : 3

x : 6 y : 4

x : 6 y : 5

x : 6 y : 7

x : 6 y : 8

x : 6 y : 9

x : 6 y : 10

x : 7 y : 2

x : 7 y : 6

x : 7 y : 8

x : 8 y : 2

x : 8 y : 3

x : 8 y : 4

x : 8 y : 5

x : 8 y : 6

x : 8 y : 7

x : 8 y : 9

x : 8 y : 10

x : 9 y : 2

x : 9 y : 4

x : 9 y : 6

x : 9 y : 8

x : 9 y : 10

x : 10 y : 2

x : 10 y : 3

x : 10 y : 5

x : 10 y : 6

x : 10 y : 8

x : 10 y : 9

由此可見,每個點可取的值與phi[i-1]有關,故使用篩法線性求尤拉函式,之後根據對稱性並考慮特殊點,將ans = ans * 2 + 1即為結果

#include #include #include const int maxn = 40000 + 100;

int phi[maxn], isprime[maxn], prime[maxn];

int n;

int tot = 0;

long long ans = 0;

int main ()

for (int j = 1; j <= tot; j++) else }}

// for (int i = 1; i <= n; i++) printf("phi[%d] = %d\n", i, phi[i]);

for (int i = 2; i <= n; i++)

ans = ans * 2 + 1;

printf("%lld", ans);

return 0;

}

洛谷 2158 數論 打表 尤拉函式

傳送門 其實看到sdoi就有一種不太好的預感,想當年那個豬國殺,呵呵,用二維陣列維護每個點能否被選擇,之後二維列舉每個點,如果沒有被選擇就選擇,並且用它去遮擋其他所有點 include include include const int maxn 1000 10 int a maxn maxn in...

數論 尤拉函式

尤拉函式,用 n 表示 尤拉函式是求小於等於n的數中與n互質的數的數目 求n的尤拉函式時我們可以減去它的所有素數因子以及它的倍數 12 12 2 2 3 素數因子有2 3所以我們減掉2,3的倍數 2的倍數 2,4,6,8,10,12 3的倍數 3,6,9,12 顯然2,3存在重複項6,12 根據容斥...

數論 尤拉函式

ll eular ll n if n 1 ans ans n n 1 return ans 尤拉函式的一些性質 當m,n互質時,有phi m n phi m phi n 若i p 0,有phi i p p phi i 對於互質x與p,有x phi 1 mod p 因此x的逆元為x phi 1 即尤拉...