几道水题

灰魔法师:

 

给出长度为n的序列a, 求有多少对数对 (i, j) (1 <= i < j <= n) 满足 ai + aj 为完全平方数。

第一行一个整数 n (1 <= n <= 105)
第二行 n 个整数 ai (1 <= ai <= 105)

// 给出长度为n的序列a, 求有多少对数对 (i, j) (1 <= i < j <= n) 满足 ai + aj 为完全平方数。

#include<bits/stdc++.h>

using namespace std;
int num;
long long ind[100010];

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {scanf("%d", &num); ind[num]++;}
    long long ans = (ind[2] * (ind[2]-1)) / 2;
    for(int i = 3; i < 100010; i++){
        if(ind[i]){
            int tmp = sqrt(i);
            tmp++;
            while((tmp*tmp) < 2*i){
                ans+=ind[(tmp*tmp) - i]*ind[i];
                tmp++;
            }
            if((tmp*tmp) == 2*i) ans += (ind[i] * (ind[i]-1)) / 2;

        }
    }
    //cout << ind[1]<<" " << ind[2] <<endl;
    cout << ans <<endl;
    return 0;
}

 

POJ2409

Polya定理入门题:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int gcd(int a, int b)
{
  if(b == 0) return a;
  else return gcd(b, a%b);
}

int  solve(int n , int m)
{
  int res = 0;
  for(int i = 1; i <= m; i++)
  {
    res += pow(n, gcd(i, m));
  }
  //cout << res << endl;
  if(m&1)
  {
    res += pow(n, m/2+1)*m;
    res /= 2*m;
  }
  else
  {
    res += (pow(n, m/2+1)*m/2+pow(n, m/2)*m/2);
    res = res/(m+m);
  }
  return res;
}




int main()
{
  int n,m;
  while(cin >> n >> m)
  {
    if(n+m == 0) break;
    int ans = solve(n, m);
    cout << ans << endl;
  }
  return 0;
}

POJ1286

继续Polya入门

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long

using namespace std;

int gcd(int a, int b)
{
  if(b == 0) return a;
  else return gcd(b, a%b);
}

void run()
{
  int n;
  while(cin >> n)
  {
    if(n == -1) break;
    if(n == 1) {cout << 3 << endl; continue;}
    if(n == 0) {cout << 0 << endl; continue;}
    ll res = 0;
    for(int i = 1; i <= n; i++)
    {
      res += pow(3, gcd(i, n));
    }
    // cout << res << endl;
    if(n&1)
    {
      res += pow(3, n/2+1)*n;
      res /= 2*n;
    }
    else
    {
      res += (pow(3, n/2+1)*n/2+pow(3, n/2)*n/2);
      res /= 2*n;
    }
    cout << res << endl;
  }
}

int main()
{
  run();
  return 0;
}

POJ3923

因为要取模, 所以除法要用到逆元

#include <iostream>
#include <algorithm>
#include <cmath>
#include<cstdio>
using namespace std;
#define ll long long
const int mod =  1e9+7;

ll gcd(ll a, ll b)
{
  if(b == 0) return a;
  else return gcd(b, a%b);
}

ll qpow(ll a ,ll b)
{
  ll ans = 1;
  while(b)
  {
    if(b&1) ans = ans*a%mod;
    a = a*a%mod;
    b >>= 1;
  }
  return ans;
}

ll euler_m(ll n){ //返回euler(n)
     ll res=n,a=n;
     for(ll i=2;i*i<=a;i++){
         if(a%i==0){
             res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
             while(a%i==0) a/=i;
         }
     }
     if(a>1) res=res/a*(a-1);
     return res;
}



ll  solve(ll n , ll m)
{
  ll res = 0;
  ll i;
  for(i = 1; i*i < m; i++)
  {
    if(m%i == 0)
    {
      res = (res+qpow(n, m/i)*euler_m(i))%mod;
      res = (res+qpow(n, i)*euler_m(m/i))%mod;
    }
  }
  if(i*i == m) res = (res+qpow(n, i)*euler_m(m/i))%mod;
  //cout << res << endl;
  if(m&1)
  {
    res += qpow(n, m/2+1)*m;
    res %= mod;
    res = (res*qpow(2*m, mod-2))%mod;   // 费马小定理求逆元
  }
  else
  {
    res += (qpow(n, m/2+1)*m/2+qpow(n, m/2)*m/2);
    res %= mod;
    res = (res*qpow(2*m, mod-2))%mod;
  }
    res %= mod;
  return res;
}




int main()
{
  ll n,m,t;
  cin >>t;
  ll j = 0;
  while(j++ < t)
  {
    cin >> n >> m;
    if(n+m == 0) break;
    ll ans = solve(n, m);
    cout << "Case #" << j << ": ";
    printf("%lld\n", ans);
  }
  return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值