zoukankan      html  css  js  c++  java
  • CF#716 div2

    B. AND 0, Sum Big
    构造n个数,它们相对应的位置&(与)后为0,和为最大。
    如果n=4,k=3;
    110,101,011,111符合
    因为和要最大,所以每个位置只能有1个0,其他都为1。
    每个位置的0都有n种选择,所以答案为n的次方。
    大佬的快速幂算法详解

    代码
    
    const ll m = 1e9 + 7;
    ll fastPower(ll base, ll power) {
        ll result = 1;
        while (power > 0) {
            if (power & 1) { //此处等价于if(power%2==1)
                result = result * base % m;
            }
            power >>= 1;//此处等价于power=power/2
            base = (base * base) % m;
        }
        return result;
    }
    int main()
    {
    	ios::sync_with_stdio(false); cin.tie(nullptr);
    	int t;
    	cin >> t;
    	while (t--) {
    		ll n, k;
            cin >> n >> k;
            cout << fastPower(n, k) << endl;
    	}
    }
      

    C. Product 1 Modulo N(有点巧妙/数论)
    求一个子数列,连乘积模n为1
    (prod=k imes n+1, gcd(prod,n)=1;)
    连乘积和n的gcd为1,所以因子肯定和n互质,没有公因子。除去拥有公因子的数。
    将剩下的乘在一起,如果prod%n==1,则直接符合题意
    否则 prod%n=s prod=kn+s
    gcd(s,n)=gcd(s+k
    n,n)=1,所以s肯定属于因子,直接删去s即可

    代码
    
    const int N = 1e5 + 5;
    int gcd(int x,int y){
    	if (y == 0)
    		return x;
    	return gcd(y, x % y);
    }
    int main()
    {
    	ios::sync_with_stdio(false); cin.tie(nullptr);
    	int n;
    	cin >> n;
    	bool f[N];
    	memset(f, true, sizeof(f));
    	ll prod = 1;
    	int sum = 0;
    	for (int i = 1; i < n; i++) {
    		if (gcd(i, n) != 1)
    			f[i] = false;
    		else {
    			prod = prod * i % n;     //prod有可能太大了
    			sum++;
    		}
    	}
    	if (prod%n != 1) {
    		f[prod%n] = false;
    		cout << sum - 1 << endl;
    	}
    	else
    		cout << sum << endl;
    	for (int i = 1; i < n; i++) {
    		if (f[i])
    			cout << i << " ";
    	}
    }
      
  • 相关阅读:
    Add Two Numbers
    Reverse Linked List II
    Reverse Linked List
    Remove Duplicates from Sorted List
    Remove Duplicates from Sorted List II
    Partition List
    Intersection of Two Linked Lists
    4Sum
    3Sum
    2Sum
  • 原文地址:https://www.cnblogs.com/lingshi321/p/14685961.html
Copyright © 2011-2022 走看看