小编Dan*_*elV的帖子

如何在不到1秒的时间内计算2 ^ x mod n = 1

我想写一个程序,计算2^x mod n = 1我们有n一个integer但是,我们应该计算x.我写了代码,但是我的代码在大n中运行得太慢了.你能建议我一个不到1秒的工作来解决这个问题.
这是我的代码:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    long long int n,cntr=1,cheak;
    cin >> n;
    while (1)
    {
        if (n % 2 == 0)
        {
            break;
        }
        cheak=pow(2, cntr);
        if (cheak % n == 1)
            break;
        cntr++;
    }
    cout << cntr << endl;
} 
Run Code Online (Sandbox Code Playgroud)

c++ math

5
推荐指数
1
解决办法
822
查看次数

如何正确使用getline

我编写了一个使用getline()函数的程序,但我不知道如何getline()以正确的方式使用它.我搜索在互联网上,但我只是发现了一些关于如何使用getchar()带有getline()cin.ignore().我试过,但没有一个工作,我不知道是什么问题,为什么getline()功能需要getchar()cin.ignore()正常工作?

我的意见

2   
Alderaan   
1000.00 2000.00 3000.00   
Dantooine   
-1000.00 1000.00 1000.00   
Circarpous Major   
-500.00 500.00 -500.00   
Y’Toub   
-500.00 -500.00 500.00    
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#include <cmath>
#include <string>
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    long long int  x, y, z, x1, y1, z1;
    int n;
    cin >> n;
    string s, p;
    while (n--)
    {
        getline(cin, s);
        cin >> x;
        cin >> y;
        cin …
Run Code Online (Sandbox Code Playgroud)

c++ getline

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×2

getline ×1

math ×1