zoukankan      html  css  js  c++  java
  • D. Unusual Sequences(容斥)

    D. Unusual Sequences

    隔板法 + 容斥原理

     1 //容斥(莫比乌斯反演)
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4 #define LL long long 
     5 const int mod = 1e9+7;
     6 LL quickpow(LL a, LL b, LL mod){
     7     LL temp = a % mod, res = 1;
     8     while(b){
     9         if(b & 1)  res = res * temp % mod;
    10         b >>= 1;
    11         temp = temp * temp % mod;
    12     }
    13     return res;
    14 }
    15 map<int, LL> mp;
    16 
    17 LL solve(int x){
    18     if(x == 1) return 1;
    19     if(mp.count(x)) return mp[x];
    20     mp[x] = quickpow(2, x-1, mod);
    21     for(int i = 2; i * i <= x; i++){
    22         if(x % i == 0){
    23             mp[x] = (mp[x] - solve(i) + mod) % mod;
    24             if(i != x / i) mp[x] = (mp[x] - solve(x / i) + mod) % mod;
    25         }
    26     }
    27     mp[x] = (mp[x] - solve(1) + mod) % mod;
    28     return mp[x];
    29 }
    30 
    31 int main(){
    32     ios::sync_with_stdio(0);
    33     int x, y;
    34     while(cin>>x>>y){
    35         if(y % x != 0){
    36             cout<<0<<endl;
    37         }else{
    38             cout<<solve(y / x)<<endl;
    39 
    40         }
    41     }
    42 
    43 }
    View Code
  • 相关阅读:
    IDEA
    SpringCloud
    Docker
    Docker
    JDK排序
    选择排序
    冒泡排序
    计算一个整数数组的平均数
    (if语句)中国的个税计算方法
    读入一个表示年份的整数,判断这一年是否是闰年
  • 原文地址:https://www.cnblogs.com/yijiull/p/8322024.html
Copyright © 2011-2022 走看看